![](/img/trans.png)
[英]How to set a value in hidden field according to item selected in drop down list
[英]How to set value for hidden field based on selected option in drop down?
我在JQuery中創建了我的下拉列表,在這里我從數組中讀取值和名稱。 該部分工作正常,現在我想為放置在選擇標記上方的隱藏字段設置值。 因此,每次更改選項時,都必須為該隱藏字段設置不同的值。 這是我的代碼:
HTML:
<td>
<input type="hidden" id="userID" name="user" value=""/>
<select id="selUser" name="selUser">
<option value="">--Select Student--</option>
</select>
</td>
jQuery的:
$( document ).ready(function() {
var userValues = [];
userValues.push({'idOne':"31",'idTwo':"Tom, Poitras",'idThree':"88"});
userValues.push({'idOne':"16",'idTwo':"All, Dirks",'idThree':"89"});
userValues.push({'idOne':"15",'idTwo':"John, Reed",'idThree':"50"});
for(var i=0; i < studValues.length; i++){
$('#selUser').append('<option value='+userValues[i].idOne+'>'+userValues[i].idTwo+'</option>');
}
});
在下拉菜單中選擇學生后,如何設置隱藏值(數組中的idThree)? 我應該為此使用其他功能還是針對此問題采用其他方法? 如果有人可以幫忙,請告訴我。
我建議使用jQuery的.change()事件。
此處的文檔: https : //api.jquery.com/change/
像$('#selUser').change(function() { // what you want to do here });
在document.ready()函數內部,會將匿名事件附加到下拉列表中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.