[英]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.