jQuery 分组元素并创建选择标签
好的,这就是我所拥有的
<div>
<h2 class="type">A</h2>
<p class="value">12</p>
</div>
<div>
<h2 class="type">A</h2>
<p class="value">24</p>
</div>
<div>
<h2 class="type">B</h2>
<p class="value">35</p>
</div>
,我想做的是浏览它们,将它们分组并创建一个像这样的选择下拉列表:
<select>
<option value="12,24">A</option>
<option value="35">B</option>
</select>
您将如何处理?
Ok here's what i have
<div>
<h2 class="type">A</h2>
<p class="value">12</p>
</div>
<div>
<h2 class="type">A</h2>
<p class="value">24</p>
</div>
<div>
<h2 class="type">B</h2>
<p class="value">35</p>
</div>
And what i want to do is go through them, group them and create a select dropdown like this:
<select>
<option value="12,24">A</option>
<option value="35">B</option>
</select>
How would you approach that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,今晚我想在睡觉前写一些 jQuery 代码来娱乐一下。
基本上,迭代您的
并根据其类型和值创建数组对象。之后,创建一个
jsFiddle 演示
Well, I felt like writing some jQuery code tonight for kicks before I go to bed.
Basically, iterate through your
<div>
s and make an object of arrays out of their types and values. After that, make a<select>
, iterate through the object and add dropdown<option>
s containing its types and values.jsFiddle demo
类似于:
现在您可以将它们放在
options
中以便于操作。Something like:
Now you have the them in
options
for easy manipulation.