简体   繁体   English

如何使用jQuery从选择框中删除一些选项?

[英]How to remove few options from select box using jquery?

I want to remove few options, say, last three options, from a select box out of 5 options, using jquery 我想使用jQuery从5个选项的选择框中删除一些选项,例如最后三个选项

Below is the select box with 5 options tag 以下是带有5个选项标签的选择框

<select id="select">
  <option>first</option>
  <option>second</option>
  <option>third</option>
  <option>fourth</option>
  <option>fifth</option>
</select>

UPDATE: 更新:

I tried this code 我尝试了这段代码

$("#select option:not(option:first, option:last)";

But that gave me uncaught exception in console 但这给了我控制台未捕获的异常

slice()方法是解决方法:

$("#select > option").slice(-3).remove();

What have you tried? 你尝试了什么? I think the nth-child selector might be what you're looking for. 我认为第n个子选择器可能是您要寻找的。

您可以使用以下代码删除最新的项目http://jsfiddle.net/QxmvC/

     $('#select').children().slice(-3).remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM