Deleting Multiple Values From Listbox in JavaScript

本文介绍了一种使用JavaScript从列表框中删除多个选定项的方法。通过逆向迭代列表框选项并删除选中项,避免了因索引变化导致的删除错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章源自:http://viralpatel.net/blogs/javascript-delete-multiple-values-options-listbox/

Deleting Multiple Values From Listbox in JavaScript

There was a requirement of deleting multiple options from a listbox using JavaScript. User can select multiple items from a Listbox. Once the delete button is pressed, remove all the values that are selected from the combobox.

Now the most intuitive solution for this problem would be to iterate through each option from the listbox and delete it if it is selected. So one would write following piece of HTML & JavaScript.

The HTML

Define a simple listbox with id lsbox and two buttons, Delete & Reset.

<table>
<tr>
	<td align="center">
		<select id="lsbox" name="lsbox" size="10" multiple>
			<option value="1">India</option>
			<option value="2">United States</option>
			<option value="3">China</option>
			<option value="4">Italy</option>
			<option value="5">Germany</option>
			<option value="6">Canada</option>
			<option value="7">France</option>
			<option value="8">United Kingdom</option>
		</select>
	</td>
</tr>
<tr>
	<td align="center">
		<button οnclick="listbox_remove('lsbox');">Delete</button>
		<button οnclick="window.location.reload();">Reset</button>
	</td>
</tr>
</table>

 Below is the JavaScript code.

The (Incorrect) JavaScript

function listbox_remove(sourceID) {

    //get the listbox object from id.
    var src = document.getElementById(sourceID);
 
    //iterate through each option of the listbox
    for(var count = 0; count < src.options.length; count--) {
         //if the option is selected, delete the option
        if(src.options[count].selected == true) {
 
                try {
                         src.remove(count, null);
                        
                 } catch(error) {
                        
                         src.remove(count);
                }
        }
    }
}

 That’s it right? Wait, this wont work correctly. Each time you delete an option using src.remove() method, the remaining option’s index will decrease by 1. So if you have selected multiple values to delete, this wont behave as you expected.

The Correct JavaScript

Notice the loop highlighted line. We are iterating in opposite direction.

function listbox_remove(sourceID) {

    //get the listbox object from id.
    var src = document.getElementById(sourceID);
 
    //iterate through each option of the listbox
    for(var count= src.options.length-1; count >= 0; count--) {

         //if the option is selected, delete the option
        if(src.options[count].selected == true) {
 
                try {
                         src.remove(count, null);
                        
                 } catch(error) {
                        
                         src.remove(count);
                }
        }
    }
}

 Following is the demo with correct JavaScript.

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值