Home >Web Front-end >JS Tutorial >How to delete multiple items from listbox at the same time using JavaScript_javascript skills

How to delete multiple items from listbox at the same time using JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:20:141289browse

To delete multiple items from the list box at the same time, we cannot delete from top to bottom, because every time an upper item is deleted, the index number of the lower item will change, so we can only delete from bottom to top, so that it will not appear There is a problem with index numbers changing randomly.

html code

Copy code The code is as follows:















The javascript code is as follows:
Copy code The code is as follows:

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);
}
}
}
}

Of course, if you use jQuery to delete, it will be convenient, in one sentence Done ('option:selected').remove();
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn