简体   繁体   English

使用jQuery从多个选择元素中删除最后一个选项值

[英]Remove last option value from multiple select element using jQuery

I have two select element and I need to remove last option from both select using jQuery. 我有两个选择元素,我需要使用jQuery从两个选择中删除最后一个选项。

First Select Element 首选元素

<select class="selBooks" name="select1" >
  <option value="8247(random)">Opt2</option>
  <option value="1939(random)">Opt1</option>
</select>

Second Select Element 第二选择元素

<select class="selBooks" name="select2" >
  <option value="8244(random)">Opt3</option>
  <option value="1938(random)">Opt4</option>
</select>

jQuery jQuery的

$(".selBooks option:last").remove();

When I try this it only removes last option of second select element. 当我尝试这样做时,它只会删除第二个选择元素的最后一个选项。 Please guide me if I am doing anything wrong or there is other way to achieve this. 如果我做错了任何事情,或者有其他方法可以解决这个问题,请指导我。

iterate .selBooks with loop and remove the last element of that class. 使用循环迭代.selBooks并删除该类的最后一个元素。

 $('.selBooks').each(function() { $(this).find("option:last").remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="selBooks" name="select1" > <option value="8247(random)">Opt2</option> <option value="1939(random)">Opt1</option> </select> Second Select Element <select class="selBooks" name="select2" > <option value="8244(random)">Opt3</option> <option value="1938(random)">Opt4</option> </select> jQuery 

Try by name name尝试

  $(".selBooks[name=select1] option:last").remove(); $(".selBooks[name=select2] option:last").remove(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="selBooks" name="select1" > <option value="8247(random)">Opt2</option> <option value="1939(random)">Opt1</option> </select> Second Select Element <select class="selBooks" name="select2" > <option value="8244(random)">Opt3</option> <option value="1938(random)">Opt4</option> </select> 

Use last-child . 使用last-child

 $('.selBooks option:last-child').remove(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select class="selBooks" name="select1"> <option value="8247(random)">Opt2</option> <option value="1939(random)">Opt1</option> </select> <select class="selBooks" name="select2"> <option value="8244(random)">Opt3</option> <option value="1938(random)">Opt4</option> </select> 

暂无
暂无

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

相关问题 如果用户使用jquery选择3个以上选项,则从多个选择中删除最后选择的元素 - Remove last selected element from the multiple select if user select more than 3 option using jquery jQuery 获取多个 select 列表中最后选择的选项的值 - jQuery get value of last selected option in multiple select list jQuery选项从多个选择框中删除 - JQuery option remove on select from multiple select box 如何根据另一个select html元素的选项值使用jQuery遍历select html选项 - How to iterate through select html options using jQuery depending on option value from another select html element 对于select2多个中的每个选项,请使用jquery将选项值文本附加到div中 - for each option from select2 multiple, append option value text into div using jquery 用jQuery取消选择多个选择中的最后一个选择的选项 - Deselect last selected option in multiple select with jQuery 从多个动态记录中使用$(document).ready函数在jQuery中获取选择选项值? - get select option value using $(document).ready function in jQuery from multiple dynamic record? 从多个动态记录中使用jQuery中的onchange函数获取选择选项值? - get select option value using onchange function in jQuery from multiple dynamic record? 使用 jquery 在多个选择标签中获取选项值 - Get option value in multiple select tag using jquery jquery删除&#39;select option&#39;元素值,但第二个选项值除外 - jquery delete 'select option' element value except the second option value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM