jquery 获取数组的选择选项
我有点坚持一个 - 大概很容易解决问题。
我想创建将用于网站上所有选择元素的代码。
根据分配给“选项”的“标签”属性,我想隐藏所有其他“div”元素,这些元素的类与此特定“选择”菜单中“选项”的其他“标签”值相同。
让我演示一下:
<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>
现在 - 菜单下的 div 中的“dn”类仅分配了“display:none”。
根据所选选项 - 如果它的值为 3 - 我希望与要显示的标签具有相同类的 div - 那么如果我选择值为 4 的选项 - 所有其他 div(其中类名将从所有选项填充)这个选择元素)应该隐藏()并且只选择显示()。
我不太确定如何将特定“选择”元素的所有“选项”元素放入数组中。 然后我想我可以使用each()语句循环它并找出它们是否有“标签”,如果有的话-隐藏与它的值匹配的类的元素。
然后在循环之后,我可以显示具有与所选“选项”的“标签”参数值匹配的类的元素。
知道如何实现这一目标吗?
I'm kind of stuck with one - presumably simple to resolve problem.
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.
Let me demonstrate:
<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>
Now - "dn" class in the div under the menu has simply "display:none" assigned.
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().
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给与特定选择相关的所有 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: