[英]How to set value for hidden field based on selected option in drop down?
I have my drop down created in JQuery where I read values and names from array. 我在JQuery中创建了我的下拉列表,在这里我从数组中读取值和名称。 That part works fine, now I would like to set value for hidden field that I placed above my select tag.
该部分工作正常,现在我想为放置在选择标记上方的隐藏字段设置值。 So each time I change my option I have to set different value for that hidden field.
因此,每次更改选项时,都必须为该隐藏字段设置不同的值。 Here is my code:
这是我的代码:
HTML: HTML:
<td>
<input type="hidden" id="userID" name="user" value=""/>
<select id="selUser" name="selUser">
<option value="">--Select Student--</option>
</select>
</td>
JQuery: 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>');
}
});
How I can set hidden value(idThree in my array) after I pick the student in my drop down? 在下拉菜单中选择学生后,如何设置隐藏值(数组中的idThree)? Should I have another function for that or there is some another approach for this problem?
我应该为此使用其他功能还是针对此问题采用其他方法? If anyone can help please let me know.
如果有人可以帮忙,请告诉我。
I'd suggest using jQuery's .change() event. 我建议使用jQuery的.change()事件。
Documentation here: https://api.jquery.com/change/ 此处的文档: https : //api.jquery.com/change/
Something like $('#selUser').change(function() { // what you want to do here });
像
$('#selUser').change(function() { // what you want to do here });
inside your document.ready() function would attach an anonymous event to the dropdown. 在document.ready()函数内部,会将匿名事件附加到下拉列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.