使用jQuery将选择选项ID值复制到隐藏字段中 [英] Copy select option ID value into hidden field with jQuery

查看:58
本文介绍了使用jQuery将选择选项ID值复制到隐藏字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些教程,您可以在其中将select选项的值复制到这样的隐藏字段中:

I've seen a few tutorials where you can copy the value of a select option into a hidden field like this:

$("#my_stuff").change(function(){
    $("#my_hidden_field").val($("select option:selected").val());
});

所以,如果我有这个...

So if I had this...

<select name="my_stuff" id="my_stuff">
    <option value="Red">Red things</option>
    <option value="Green">Green things</option>
    <option value="Blue">Blue things</option>
</select>

...并选择红色事物",然后将红色事物"复制到隐藏字段值中.

...and selected 'Red things', then 'Red things' would be copied into the hidden field value.

但是我真正需要做的是复制 ID ,因此,如果我有这个...

But what I really need to do is copy the ID instead, so if I had this...

<select name="my_stuff" id="my_stuff">
    <option value="Red" id="all the red stuff">Red things</option>
    <option value="Green" id="all the green stuff">Green things</option>
    <option value="Blue" id="all the blue stuff">Blue things</option>
</select>

...然后我选择了红色的东西",然后将所有红色的东西"复制到隐藏字段中.

...and I selected 'Red things', then 'all the red stuff' would be copied into the hidden field.

这有可能吗?

推荐答案

非常简单:在这里,您去了:

Its very simple: Here you go:

$("#my_stuff").change(function(){
    $("#my_hidden_field").val($("select option:selected").attr('id'));
});

这篇关于使用jQuery将选择选项ID值复制到隐藏字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆