jQuery 获取选择选项 ID 并更改隐藏的输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3810718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:09:03  来源:igfitidea点击:

jQuery get select option id and change hidden input value

jquery

提问by richardpixel

I have a form with a select list. Each option also contains a dynamic id, which I need to capture and then use that to change a hidden input's value. So basically take the selected options id and change the value of a hidden input value.

我有一个带有选择列表的表单。每个选项还包含一个动态 ID,我需要捕获它,然后使用它来更改隐藏输入的值。所以基本上采用选定的选项 id 并更改隐藏输入值的值。

My select and hidden input look like:

我的选择和隐藏输入看起来像:

<select name="item_options" id="size">
<option value="20030" id="Universal">Universal (20030)</option>
<option value="4545456" id="Medium">Medium (4545456)</option>
<option value="15447" id="Large">Large (15447)</option>
</select>

<input type="hidden" name="item_options_name" value="Universal" id="changevalue" />

I had done some jQuery to capture the selected option's id, but I can't figure out how to use it to change my input's value.

我已经做了一些 jQuery 来捕获所选选项的 id,但我不知道如何使用它来更改我的输入值。

回答by Reigel

$('#size').change(function(){
   var id = $(this).find(':selected')[0].id;
   $('#changevalue').val(id);
})

回答by Gazler

$('select[name=item_options]').change(function(){
    $('input[name=item_options_name]').val($(this).val());
};

Or you can use ID selectors:

或者您可以使用 ID 选择器:

$('#size').change(function(){
    $('#changevalue').val($(this).val());
};

回答by Manjunatha. K

<script type="text/javascript">

$('#selectboxsize').change(function() {

    var id = $(this).find(':selected')[0].id;

    $('#getchangevalue').val(id);

});

</script>




<select name="item_options" id="selectboxsize">

    <option value="20030" id="Universal">Universal (20030)</option>

    <option value="4545456" id="Medium">Medium (4545456)</option>

    <option value="15447" id="Large">Large (15447)</option>

</select>

<input type="hidden" name="item_options_name" value="Universal" id="getchangevalue" />