得到的jQuery选择选项数组 [英] jquery get select options to array
问题描述
我有点坚持一个 - presumably简单的解决问题。
I'm kind of stuck with one - presumably simple to resolve problem.
我要创建code将被用于网站上的所有选择元素。
I want to create the code which will be used for all select elements on the site.
根据分配到选项我想隐藏哪个上课一样在这个特定的选择菜单中的选项其他标签值的所有其他的格元素的标签属性。
Depending on the "label" attribute assigned to the "option" I want to hide all other "div" elements which have classes the same as other "label" values of the "options" in this specific "select" menu.
让我演示一下:
<select class="sel_depend" id="reg_where_heard" name="where_heard">
<option value="">Select one…</option>
<option value="1">Google</option>
<option value="2">Yahoo</option>
<option value="3" label="where_heard_magazine">Magazine</option>
<option value="4" label="where_heard_other">Other</option>
</select>
<div class="where_heard_magazine dn"><input type="text" name="magazine" id="magazine" value="" /></div>
<div class="where_heard_other dn"><input type="text" name="other" id="other" value="" /></div>
现在 - 在菜单下的格DN类有简单的显示:无分配
Now - "dn" class in the div under the menu has simply "display:none" assigned.
根据所选的选项 - 如果它的值是3 - 我想与类一样的标签来显示DIV - 那么如果我选择用价值4选项 - 其他所有的div(如类名称将从填充)这种选择元素的所有选项)应该隐藏(也是唯一的选择显示()。
Depending on the selected option - if it's value is 3 - I want the div with the class the same as the label to show - then if I select option with value 4 - all other divs (where class names would be populated from all options of this select element) should hide() and only selected show().
我不太清楚如何把具体的选择元素的所有选项元素的数组。
然后presumably我可以通过它使用循环每()语句,并找出他们是否有标签,如果是这样 - 隐藏与类匹配其值的元素
I'm not quite sure how to put all "option" elements of the specific "select" element to array. Then presumably I could loop through it using each() statement and find out whether they have "label" and if so - hide the element with the class matching its value.
则循环后,我可以显示与所选择的选项中的标签参数的值匹配类的元素。
then after the loop I could show the element with the class which matches the value of the "label" parameter of the selected "option".
我希望是有道理的。
任何想法如何实现这一目标?
Any idea how to achieve this?
非常感谢。
推荐答案
让所有与特定的div中选择一类是元素的名称。那么你可以这样做:
give all the divs related to a specific select a class that is the name of that element. then you can do something like:
$('.sel_depend').change(function(){
var class = $(this).attr('name');
$('.' + class).hide();
var divToShowClass = $(this).find(':selected').attr('label');
$('.' + divtoShowClass).show();
});
这篇关于得到的jQuery选择选项数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!