如何删除选择框的所有选项,然后添加一个选项并使用 jQuery 选择它?
使用核心 jQuery,如何删除选择框的所有选项,然后添加一个选项并选择它?
我的选择框如下。
<Select id="mySelect" size="9"> </Select>
编辑:以下代码对链接很有帮助。 但是,(在 Internet Explorer 中).val('whatever')
未选择添加的选项。 (我确实在 .append
和 .val
中使用了相同的“值”。)
$('#mySelect').find('option').remove().end()
.append('<option value="whatever">text</option>').val('whatever');
编辑:尝试让它模仿此代码,每当页面/表格已重置。 该选择框由一组单选按钮填充。 .focus()
更接近,但该选项并未像 .selected= "true"
那样显示为选中状态。 我现有的代码没有任何问题 - 我只是想学习 jQuery。
var mySelect = document.getElementById('mySelect');
mySelect.options.length = 0;
mySelect.options[0] = new Option ("Foo (only choice)", "Foo");
mySelect.options[0].selected="true";
编辑:所选答案接近我需要的。 这对我有用:
$('#mySelect').children().remove().end()
.append('<option selected value="whatever">text</option>') ;
但这两个答案都引导我找到了最终的解决方案。
Using core jQuery, how do you remove all the options of a select box, then add one option and select it?
My select box is the following.
<Select id="mySelect" size="9"> </Select>
EDIT: The following code was helpful with chaining. However, (in Internet Explorer) .val('whatever')
did not select the option that was added. (I did use the same 'value' in both .append
and .val
.)
$('#mySelect').find('option').remove().end()
.append('<option value="whatever">text</option>').val('whatever');
EDIT: Trying to get it to mimic this code, I use the following code whenever the page/form is reset. This select box is populated by a set of radio buttons. .focus()
was closer, but the option did not appear selected like it does with .selected= "true"
. Nothing is wrong with my existing code - I am just trying to learn jQuery.
var mySelect = document.getElementById('mySelect');
mySelect.options.length = 0;
mySelect.options[0] = new Option ("Foo (only choice)", "Foo");
mySelect.options[0].selected="true";
EDIT: selected answer was close to what I needed. This worked for me:
$('#mySelect').children().remove().end()
.append('<option selected value="whatever">text</option>') ;
But both answers led me to my final solution..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(28)
尝试
或
Try
OR
尝试
Try
最短的答案:
The shortest answer:
我使用了普通的 javascript
I used vanilla javascript
希望它能起作用
Hope it will work
清洁工给我点个赞
Cleaner give me Like it
我在 Select2 中看到了这段代码 -
清除选择
即使没有 jQuery,此代码也能很好地工作选择2
I saw this code in Select2 -
Clearing Selections
This code works well with jQuery even without Select2
你可以简单地通过替换 html 来完成
You can do simply by replacing html
这会将现有的 mySelect 替换为新的 mySelect。
This will replace your existing mySelect with a new mySelect.
使用 jquery prop() 清除所选选项
Uses the jquery prop() to clear the selected option
首先清除所有现有选项,执行第一个(--Select--)
使用循环一附加新选项值一个
First clear all exisiting option execpt the first one(--Select--)
Append new option values using loop one by one
另一种方式:
在此链接下,有一些很好的做法 https://api.jquery.com/select/
Another way:
Under this link, there are good practices https://api.jquery.com/select/
第一行代码将删除选择框的所有选项,因为没有提到选项查找条件。
第二行代码将添加具有指定值(“testValue”)和文本(“TestText”)的选项。
The first line of code will remove all the options of a select box as no option find criteria has been mentioned.
The second line of code will add the Option with the specified value("testValue") and Text("TestText").
基于 mauretto 的答案,这更容易阅读和理解:
要删除除具有特定值的选项之外的所有选项,您可以使用以下命令:
如果要添加的选项已经存在,这会更好。
Building on mauretto's answer, this is a little easier to read and understand:
To remove all the options except one with a specific value, you can use this:
This would be better if the option to be added was already there.
将 html 更改为新数据怎么样?
另一个例子:
How about just changing the html to new data.
Another example:
我在网上发现了类似下面的内容。 像我的情况一样,有数千个选项,这比 jQuery 的
.empty()
或.find().remove()
快得多。更多信息此处。
I've found on the net something like below. With a thousands of options like in my situation this is a lot faster than
.empty()
or.find().remove()
from jQuery.More info here.
感谢我收到的答案,我能够创建如下所示的内容,以满足我的需求。 我的问题有点含糊。 感谢您的关注。 我的最后一个问题是通过在我想要选择的选项中包含“选定”来解决的。
Thanks to the answers I received, I was able to create something like the following, which suits my needs. My question was somewhat ambiguous. Thanks for following up. My final problem was solved by including "selected" in the option that I wanted selected.
不确定“添加一个并选择它”到底是什么意思,因为无论如何它都会被默认选择。 但是,如果您要添加多个,那就更有意义了。 怎么样:
对“编辑”的响应:您能否澄清“此选择框由一组单选按钮填充”的含义?
元素。
Not sure exactly what you mean by "add one and select it", since it will be selected by default anyway. But, if you were to add more than one, it would make more sense. How about something like:
Response to "EDIT": Can you clarify what you mean by "This select box is populated by a set of radio buttons"? A
<select>
element cannot (legally) contain<input type="radio">
elements.如果您的目标是从选择中删除除第一个选项之外的所有选项(通常是“请选择一个项目”选项),您可以使用:
If your goal is to remove all the options from the select except the first one (typically the 'Please pick an item' option) you could use:
我在 IE7 中遇到了一个错误(在 IE6 中工作正常),使用上述 jQuery 方法会清除 DOM 中的 select 但不会清除屏幕上的。 使用 IE 开发工具栏,我可以确认 select 已被清除并具有新项目,但在视觉上 select 仍然显示旧项目 - 即使您无法选择他们。
修复方法是使用标准 DOM 方法/属性(如海报原文所示)来清除而不是 jQuery - 仍然使用 jQuery 来添加选项。
I had a bug in IE7 (works fine in IE6) where using the above jQuery methods would clear the select in the DOM but not on screen. Using the IE Developer Toolbar I could confirm that the select had been cleared and had the new items, but visually the select still showed the old items - even though you could not select them.
The fix was to use standard DOM methods/properites (as the poster original had) to clear rather than jQuery - still using jQuery to add options.
为什么不直接使用普通的 javascript 呢?
why not just use plain javascript?