具有多个具有相同值的条目的 JQuery 选择选项
我有一个程序,有一个下拉菜单,可以从国家列表中进行选择。在此列表中,我选择了将出现在列表顶部的 N 个“重要”国家/地区,后面是所有国家/地区的列表,包括出现在“重要”国家/地区部分的国家/地区名单。
例如:
<select id="id-search-country" name="country">
<option value="----">All countries
<option value="AR,,">Argentina # you can see that this is repeated
<option value="AU,,">Australia
<option value="BR,,">Brazil
<option value="CA,,">Canada
<option value="----">----
<option value="----">All countries
<option value="AF,,">Afghanistan
<option value="AL,,">Albania
<option value="DZ,,">Algeria
<option value="AS,,">American Samoa
<option value="AR,,">Argentina # repeated here
etc.
当我尝试使用 $(#id-search-country).val(current_country_value) 设置用户当前国家/地区的值时,JQuery 将选择列表中的最后一项,而不是选择顶部的值列表中的。我希望它选择出现在列表顶部的国家/地区值(如果它出现在那里)。
有谁知道我如何配置 JQuery 将当前国家/地区选项设置为“重要”国家/地区(如果 current_country_value 位于列表的该部分),并且仅从列表的其余部分中选择国家/地区(如果未出现)在“重要”部分?
亲切的问候
I have a program that has a drop-down menu for selecting from a list of countries. Within this list, I have selected N "important" countries that will appear at the top of the list, followed by a listing of all countries, including the countries that appeared in the "important" country part of the list.
For example:
<select id="id-search-country" name="country">
<option value="----">All countries
<option value="AR,,">Argentina # you can see that this is repeated
<option value="AU,,">Australia
<option value="BR,,">Brazil
<option value="CA,,">Canada
<option value="----">----
<option value="----">All countries
<option value="AF,,">Afghanistan
<option value="AL,,">Albania
<option value="DZ,,">Algeria
<option value="AS,,">American Samoa
<option value="AR,,">Argentina # repeated here
etc.
When I try to set the value of the users current country by using $(#id-search-country).val(current_country_value), JQuery will select the last item in the list as opposed to selecting the value at the top of the list. I would prefer it to select the country value that appears at the top of the list if it appears there.
Does anyone know how I could configure JQuery to set the current country option to the "important" country (if the current_country_value is in that part of the list), and to only select the country from the remainder of the list if it has not appeared in the "important" part?
Kind Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:在 Reigel 和我在评论中互相较量后,只需一行 jQuery 即可实现:
Edit: After Reigel and I one-upped each other in the comments, it can be achieved with one line of jQuery:
基本上,为此,我使用属性选择器并查找具有刚刚选择的值的最后一个元素。对于重复项,这将是顶部的那个,对于单个项,它将是其本身。然后设置
selected
属性来告诉浏览器应该选择哪个。示例: http://jsfiddle.net/jonathon/7hgjR/
Basically, for this I use the attribute selector and look for the last element with the value that has just been selected. For duplicates, this will be the one at the top and for single ones, it'll be itself. Then set the
selected
attribute to tell the browser which should be selected.Example: http://jsfiddle.net/jonathon/7hgjR/
我想我有答案了。它可能可以缩小一点,但通过一些解决方法,这就是我得到的:
我将选项放入一个数组中,然后将值推送到一个新数组(options.indexOf('AR,,') 不会由于某种原因不起作用);然后根据其索引选择选项,以便选择第一个。
查看实际效果
I think I have an answer. It might be able to be slimmed down a bit, but with a few workarounds, here's what I got:
I made the options into an array, then pushed the values to a new array (options.indexOf('AR,,') doesn't work for some reason); then selected the option based on its index, so that the first will be selected.
See it in action
在选择国家/地区之前,您可以尝试从列表底部(完整列表)中删除“重要”国家/地区。然后将它们添加回正确的位置。
所选项目应保持不变。
You could try to remove the 'important' countries from the bottom section of the list (the full list), before selecting the country. Then add them back in the correct spots.
The selected item should remain the same.
如果所选值在列表中,则上面给出的可接受的解决方案效果很好,但如果未找到它,则会在 google crome 中导致错误,导致下拉菜单完全空白。修复如下:
The accepted solution given above works great if the selected value is in the list, but if it is not found it causes an error in google crome that results in the drop-down menu being completely blanked out. The fix is the following: