[英]Syntax for onchange event to update a hidden text box with the index value of a select box
我的意思是,我覺得自己像一個完全白痴,但即使在搜索了將近兩個小時后,我似乎無法做到這一點。
此選擇后跟隱藏文本字段:
<select name="location_id" class="select green-gradient" single>
<option value="2">New York</option>
<option value="3">London</option>
<option value="4">Singapore</option>
<option value="5">San Francisco</option>
<option value="6">Milan</option>
</select>
<input type='text' name="selected_location_id" class="hidden" value="">
//我通常使用此代碼作為復選框。 它切換隱藏文本框的內容。 我知道檢查選擇的值是不同的。 但我似乎無法做對。
$(document).ready(function(){
$('.switch').change(function( ){
if($(this).next().val() == 1){
$(this).next().val(0);
} else {
$(this).next().val(1);
}
});
})
//我試過這個:但它沒用,因為我需要對頁面上的所有選擇執行此操作。 它仍然不會更新隱藏的文本框。
$(document).ready(function(){
$('.select').change(function( ){
var selectedValue = $(this "option:selected").val();
$(this).next().val(selectedValue);
});
})
//也許我只是累了 但現在是時候尋求幫助了。 提前致謝。
我認為應該是
$('select').change ///(without the dot)
不
$('.switch').change
也不
$('.select').change
var selectedValue = $("option:selected").val();
通過將“this”取出來在jsfiddle中工作。
在偵聽器中, this
引用了該元素。 您還可以從select元素的名稱中獲取隱藏元素的名稱,因此如果它們位於以下形式中:
this.form['selected_' + this.name].value = this.value;
而且你沒有一個函數調用就完成了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.