本文實例講述了JavaScript列表框listbox全選和反選的實現方法。分享給大家供大家參考。具體分析如下:
通過JS代碼對列表框進行全選和反選是經常要操作的,非常具有實用價值。
function listboxSelectDeselect(listID, isSelect) {
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>
希望本文所述對大家的javascript程序設計有所幫助。