我想從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