欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript如何從listbox里同時(shí)刪除多個(gè)項(xiàng)目

 更新時(shí)間:2013年10月12日 17:22:09   作者:  
要從列表框同時(shí)刪除多個(gè)項(xiàng)目只能從下向上刪除,這樣就不會(huì)出現(xiàn)索引號(hào)亂變的問題了,下面有個(gè)不錯(cuò)的示例,大家可以感受下
要從列表框同時(shí)刪除多個(gè)項(xiàng)目,我們不能從上到下的刪除,因?yàn)樯厦娴捻?xiàng)目每刪除一個(gè),下面的項(xiàng)目的索引號(hào)就會(huì)變化,所以只能從下向上刪除,這樣就不會(huì)出現(xiàn)索引號(hào)亂變的問題了。

html代碼
復(fù)制代碼 代碼如下:

<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>

javascript代碼如下:
復(fù)制代碼 代碼如下:

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

當(dāng)然,如果使用jQuery來刪除,那就方便了,一句話就搞定了
復(fù)制代碼 代碼如下:

$("#sourceId").find('option:selected').remove();

相關(guān)文章

最新評(píng)論