通过JS代码对列表框进行全选和反选是经常要操作的,赶紧搜藏这段代码。
function listboxSelectDeselect(listID, isSelect) { // http://www.75271.com var listbox = document.getElementById(listID); for(var count=0; count < listbox.options.length; count++) { listbox.options[count].selected = isSelect; } }
下面是一个详细使用范例
Click below buttons to select or deselect all options from select box.<br> <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> <br> <button onclick="listboxSelectDeselect('lsbox', true);">Select All</button> <button onclick="listboxSelectDeselect('lsbox', false);">Deselect All</button> <script> function listboxSelectDeselect(listID, isSelect) { var listbox = document.getElementById(listID); for(var count=0; count < listbox.options.length; count++) { listbox.options[count].selected = isSelect; } } </script>