jquery - 如何使用jquery从选择框中删除选项

标签 jquery

如何使用jquery删除opt1行?更改 opt2 以使其被选中怎么样? 请注意,这些值是随机数。

      <select name="ShippingMethod" >
        <option value="8247(random)">Opt2</option>
        <option value="1939(random)" selected="selected">Opt1</option>
      </select>

最佳答案

这取决于你想如何选择它,to remove by text :

$("select[name=ShippingMethod] option").filter(function() { 
    return this.text == "Opt1"; 
}).remove();

Or the selected one :

$("select[name=ShippingMethod] option:selected").remove();

Or the second one :

$("select[name=ShippingMethod] option:eq(1)").remove();

Or the last one :

$("select[name=ShippingMethod] option:last").remove();
<小时/>

要通过文本选择选项 2,您可以使用相同的 .filter()上面的方法:

$("select[name=ShippingMethod] option").filter(function() { 
    return this.text == "Opt2"; 
}).attr("selected", true);

You can test it here .

关于jquery - 如何使用jquery从选择框中删除选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4313639/

相关文章:

javascript - 我们可以使用 Magnific Popup 插件来查看 .pdf 文件吗?

javascript make select 2 使用外部源设置的 url

javascript - 复选框菜单切换

javascript - 使用 Onchange 添加 css

javascript - 如何处理 jQuery UI 小部件中的事件

javascript - 如何使用不显眼的 JavaScript 将参数传递给函数?

当我单击浏览器后退按钮时,jQuery 更改单选按钮上的值

javascript - 如何停止内部有 setInterval() 的 jQuery 队列自定义任务?

javascript - 使用 Javascript PHP MySQL 的在线考试计时器

javascript - jquery 关闭时销毁模式对话框