javascript - 从 JS 向 Django 表单输入字段插入一个变量

标签 javascript python html django

我正在编写一个表格来支付员工在两个城市之间的里程数。我正在使用 Google Maps API 获取信息。我正在运行一个生成变量 drivingDistanceMiles 的脚本。我想在我的 Django 表单 mileage 中填充该字段,它保存到 python 变量 distance

这是我的表单 html 的片段:

<form action="" method="post"><% csrf_token %}
{{  form.non_field_errors  }}
<div class="fieldWrapper">
    {{  form.distance.errors  }}
    <label for="id_distance">Mileage claim:</label>
    {{  form.distance  }}
</div>

在条目上方的页面上有一个距离计算器。

在我的 forms.py distance = forms.CharField(max_length=20, required = False) 中。我将其设为 CharField 而不是 IntField,以防我的员工(不是最精通计算机的一群人)在末尾添加“miles”一词。

问题:是否可以让 drivingDistanceMiles 的值在用户将城市信息输入距离计算器时自动填充?

最佳答案

如果您使用纯 javascript(不是 jQuery 或任何框架),您可以通过 document.getElementById('id_distance').value 访问该字段.您可以通过 document.getElementById('id_distance').value = <some_var>; 设置它的值

关于javascript - 从 JS 向 Django 表单输入字段插入一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11569967/

相关文章:

python - 通过 Regex 在 Python 中匹配 "..."= "..."

python - 在 Windows 上运行良好时在 Linux 上运行 Mechanize 脚本时遇到问题

html - 在 div 左侧显示指针

javascript - 创建标签时运行js函数

javascript - 带有 ng-options 的 Angular ng-model

python - 属性错误: '_io.TextIOWrapper' object has no attribute 'nameofColumn' datatable

javascript - 在 div 中的特定 <a> 上应用 CSS

javascript - 调用 javascript 函数在 dojo require block 内不起作用(dojo 版本 1.8)

javascript 隐藏切换使用一个类,还是直接隐藏?

java - JTextPane 中的 float DIV