简体   繁体   English

如何根据下拉菜单中的所选选项设置隐藏字段的值?

[英]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.

相关问题 如何根据下拉列表中选择的项目在隐藏字段中设置值 - How to set a value in hidden field according to item selected in drop down list 使用jQuery在另一个下拉列表中根据选定的选项显示隐藏的下拉列表 - Show hidden drop down based on selected option in another drop down with jQuery 如何基于另一个下拉列表设置下拉列表的选定值? - How to set the selected value for a drop down list based on another drop down list? 根据所选选项设置输入字段的值 - Set value of input field based on selected option 根据下拉菜单中的选项为文本字段提供值 - Give textfield a value based on option selected from drop down 如何从基于ul的下拉菜单中设置选定的值到SELECTED - how to set selected value from ul li based drop down to SELECTED 根据选定的下拉值填充文本字段 - Populate text field based on the selected drop down value 如何在选择下拉列表的选项中设置所选属性 - how to set selected attribute in option for select drop down list 如何在当前时间的下拉菜单中设置自动选择的选项? - How to set auto selected option in drop-down with current time? 如何使用 Javascript 根据一些动态选择的值设置下拉列表的值? - How to set the value of a drop down list based on some dynamically selected value using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM