文章源自: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 onclick="listbox_remove('lsbox');">Delete</button> <button onclick="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.
相关推荐
13. JavaScript in Web Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 13.1 Client-Side JavaScript 307 13.2 Embedding JavaScript in HTML 311 13.3 ...
例如,通过简短的数据绑定`data-bind="autocomplete:autocompleteConfig"`就可以实现输入时自动补全的功能,而无需编写额外的JavaScript代码。 使用Knockout的好处在于它能极大地减少JavaScript开发量,通过简单的...
java java_leetcode题解之Longest Word in Dictionary through Deleting.java
New: Capability to export multiple grids to a single RTF file in TTMSFMXGridRtfIO New : Options.Filtering.MultiColumn added to perform automatic multicolumn filtering in TTMSFMXGrid Fixed : Issue ...
JavaScript 中的 `delete` 运算符是用来删除对象属性的,而不是用来删除变量或函数。在 JavaScript 中,`delete` 的行为取决于它所操作的对象类型和属性特性。 首先,我们来看一个基本的例子: ```javascript var ...
GLC (glog cleaner) is a log clear for glog written in Go. This library support for deleting old logs.
本压缩包文件“Survey Image Mixing and Deleting for Data Augmentation.zip”包含了一份详细探讨这一主题的PDF报告,主要关注了两种数据增强策略:图像混合(Image Mixing)和图像删除(Image Deleting)。...
You can use a combination of a script and tool to create a single file from multiple files. Sum Column/Selection in Column Mode This power tip demonstrates how to calculate the sum from a column of ...
解决Adding 'enum value' will prevent the debug session from continuing.zip
4. In Microsoft Access, to ensure that deleting a record from the primary table also deletes related records from a child table, you need to define Referential Integrity. This feature maintains the ...
23. Agent to copy values from 1 form to another 在同一个数据库中进行 22 24. 超越OLE – 通过COM结合MS Office与Notes应用 23 25. 读写关系数据库资料 28 26. 视图---excel,表单---word 32 27. 关于Web上的检索...
Methods for deleting a workbook from the application, which can be useful for managing workspace or removing unnecessary files. **Display Recently Opened Files (Page 27)** Techniques for displaying ...
23. Agent to copy values from 1 form to another 在同一个数据库中进行 22 24. 超越OLE – 通过COM结合MS Office与Notes应用 23 25. 读写关系数据库资料 28 26. 视图---excel,表单---word 32 27. 关于...
在JavaScript环境下,CRUD系统可以帮助开发者更高效地管理数据,通常用于前端应用程序,如网页应用或者单页应用(SPA)。下面将详细解释JavaScript中的CRUD系统及其实现。 1. 创建(Create) 创建新数据涉及向...
数据增强在深度神经网络性能提升方面扮演了重要角色。该技术通过各种手段,如dropout、正则化和图像增强,防止过拟合并提高模型的泛化能力。图像混合和删除是数据增强的一个子领域,它包括两种主要操作:一是将两张...
Android-Cloud-TagView-Plus Introduction An Android Cloud Tag Widget. You can edit the tag's style, and set listener of selecting or deleting tag....Can be created from XML file or Java code. Sample A
Gain an in-depth understanding of PHP 7 arrays. After a quick overview of PHP 7, each chapter concentrates on single, multi-dimensional, associative, and object arrays. ...