添加 optgroups 以使用 javascript 动态选择
我有一个动态填充(通过ajax)的选择框,其中包含这样的结果选项:
<select id="destination" name="destination">
<option value="london-paris">London-Paris</option>
<option value="paris-london">Paris-London</option>
<option value="london-newyork">London-New-York</option>
<option value="newyork-london">New-York-London</option>
<option value="london-berlin">London-Berlin</option>
<option value="berlin-london">Berlin-London</option>
<option value="london-helsinki">London-Helsinki</option>
<option value="helsinki-london">Helsinki-London</option>
...实际上有更多,但不是本质
我想要的是使用Javascript(使用Jquery或Mootools 可能)在列表加载之后,因此在每个组之前 - 我们添加一个 optgroup 标签,其中带有从该组的第二个选项 html 中获得的标签(实际上是破折号之前的单词):
<select id="destination" name="destination">
<optgroup label="Paris">
<option value="london-paris">London-Paris</option>
<option value="paris-london">Paris-London</option>
</optgroup>
<optgroup label="New-York">
<option value="london-newyork">London-New-York</option>
<option value="newyork-london">New-York-London</option>
</optgroup>
<optgroup label="Berlin">
<option value="london-berlin">London-Berlin</option>
<option value="berlin-london">Berlin-London</option>
</optgroup>
<optgroup label="Helsinki">
<option value="london-helsinki">London-Helsinki</option>
<option value="helsinki-london">Helsinki-London</option>
</optgroup>
</select>
尽管如此,总是有两个目的地每组。
请告知如何实施这一点。
I have a dynamically populated (by ajax) select box with resulting options like that:
<select id="destination" name="destination">
<option value="london-paris">London-Paris</option>
<option value="paris-london">Paris-London</option>
<option value="london-newyork">London-New-York</option>
<option value="newyork-london">New-York-London</option>
<option value="london-berlin">London-Berlin</option>
<option value="berlin-london">Berlin-London</option>
<option value="london-helsinki">London-Helsinki</option>
<option value="helsinki-london">Helsinki-London</option>
... there are actually more of them but not the essence
The thing i want is to group each this two option portions by optgroup using Javascript (using Jquery or Mootools maybe) after the list is loaded, so that before each of this group - we add an optgroup tag with label that we get from second option html of the group (actually the word before dash):
<select id="destination" name="destination">
<optgroup label="Paris">
<option value="london-paris">London-Paris</option>
<option value="paris-london">Paris-London</option>
</optgroup>
<optgroup label="New-York">
<option value="london-newyork">London-New-York</option>
<option value="newyork-london">New-York-London</option>
</optgroup>
<optgroup label="Berlin">
<option value="london-berlin">London-Berlin</option>
<option value="berlin-london">Berlin-London</option>
</optgroup>
<optgroup label="Helsinki">
<option value="london-helsinki">London-Helsinki</option>
<option value="helsinki-london">Helsinki-London</option>
</optgroup>
</select>
Though, there are always two destinations in each group.
Please, advise how to implement this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 jQuery 就地执行此操作:
此代码迭代 select 标记中的所有选项,并将每两个选项包装在一个 optgroup 中。它还根据选项中的文本确定将 optgroup 标记为什么。
You can do this in place using jQuery:
This code iterates over all the options in the select tag, and wraps every set of two in an optgroup. It also figures out what to label the optgroup as, based on text in the options.
这并不太棘手,您只需要稍微调整一下您的选项即可。将它们从文档流中取出,添加一个 optgroup 代替两个关联选项,并将选项附加到该 optgroup。
假设选项实际上是连续的,如您的示例所示,一个可能的、良好的旧 DOM 脚本实现如下:
This is not too tricky, you only need to move around your options a bit. Take them out of the document flow, add an optgroup in the place of the two associated options and append the options to that optgroup.
Assuming that the options are actually sequential, as in your example, a possible, good old DOM scripting implementation is as follows: