我想从JS脚本中保存一个HTML字段(用于表单)。Javascript赋值给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保存到文本字段,以便在提交表单时可以获得该信息。
'js'的哪个部分不返回预期的结果? – guest271314
我对js知之甚少,但在fillInAddress()函数中,第二行应该修改id为client_address的HTML字段,或者至少这就是我想在那里做的事情。 –
on('loaded.bs.modal')变量中的上下文是否已初始化? – muuk