Javascript 为 HTML 字段赋值
Javascript assign value to HTML field
我正在尝试从JS脚本中保存HTML字段(供以后在表单中使用)。
这是代码:
形式
<form class="new_client" id="new_client" action="/clients" accept-charset="UTF-8" method="post">
<div class="form-group">
<input class="form-input" type="hidden" name="client[city]" id="client_city">
</div>
<div class="form-group">
<input class="form-input" type="hidden" name="client[address]" id="client_address">
</div>
<div id="locationField">
<input autocomplete="off" class="autocomplete" placeholder="Enter your address" type="text">
</div>
<div class="text-center">
<button class="btn button-general ">Save</button>
</div>
</form>
还有javascript:
function configureGeoAutocomplete(context) {
if( context === undefined ) {
context = $('body');
}
var element = context.find('.autocomplete')
//It doesn't really matter what this line does, it's from Google Places API
var autocomplete = new google.maps.places.Autocomplete(
element[0], {types: ['geocode']});
autocomplete.addListener('place_changed', fillInAddress)
}
function fillInAddress() {
var client_address = autocomplete.getPlace().formatted_address;
document.getElementById("client_address").value = client_address;
}
在加载表单所在的模态时查询 javascript
jQuery(function() {
$('div.modal').on('loaded.bs.modal', function(e) {
configureGeoAutocomplete(context);
}
}
我想将该client_address保存到文本字段中,以便在提交表单时我可以拥有该信息。
听起来像
是 cookie 的绝佳候选者,如果你被允许使用它们:http://www.w3schools.com/js/js_cookies.asp。 另一个想法是在查询字符串中传递它。 它不是PII或类似的东西。 尝试在该输入上使用事件代码。 我不喜欢按"输入"!
onkeypress="if (event.keycode==13) { // do something }"
处理表单提交事件:
$(function() {
$("#new_client").submit(function(e) {
e.preventDefault();
// This is your value stored in the field.
$("#client_address").val();
});
})
显然,出于某种原因,如果我按 ID 搜索元素,它不会保存字段上的信息。如果我按类搜索:
function fillInAddress() {
var place = autocomplete.getPlace();
$(".client-address").val(place.formatted_address);
}
它按预期工作。
相关文章:
- 如何更新charts.JS值字段中的JS变量
- 为特定的隐藏字段javascript/jquery赋值
- Javascript 为 HTML 字段赋值
- 通过输入标签的 html 值字段将 javascript 变量传递到另一个页面
- 如何在 MVC5 中保存自定义值字段
- 设置隐藏值字段 Javascript 的值
- 如何确保多值字段是数组
- ExtJS 4 组合框在表单绑定上显示值字段而不是显示字段
- 如果我忽略对象在层次结构中的深度,如何为对象的字段赋值
- JSON 数据已将字段名称连接成值字段 (Django)
- HTML值字段为Javascript项
- 在文本框的值字段中填充文件内容
- 值字段的Javascript警告消息
- 将表单中的多个值字段发送到aspx web表单的高效且轻松的方式
- 如何从查找结果中设置xpages中的值字段
- 使用javascript函数参数填充Rails3中innerHTML标记的值字段
- 使用f.select选择课程,并自动输入count_stud值字段
- 图表 - 如何在自定义工具提示中格式化值字段
- 为什么我不能得到我的
- JSON对象的已知非空值字段返回空字符串