2012-09-13 188 views
0

刪除我需要從使用jQuery從多個選擇列表

多個列表中刪除項目

HTML:

<input type="checkbox" id="theCheckbox"/> 
<select id="theSelect" multiple="multiple"> 
    <option>1</option> 
    <option>2</option> 
    <option>3</option> 
</select> 

的JavaScript:

$("#theCheckbox").change(function() { 
    $("#theSelect").attr("multiple", (this.checked) ? "multiple" : ""); 
}).change(); 

This is the example.

這是我的代碼形成我的項目。如果你可以在這個代碼上實現,我會很高興! My Code

+2

你有什麼問題?你想實現什麼?你試過什麼了?請不要成爲「火和忘記」 - cannon;) – Luceos

+1

什麼是問題? –

回答

2

只需選擇要刪除的選項,並調用其remove()功能:

$('#theSelect option:eq(1)').remove(); 

http://jsfiddle.net/DdhSF/162/

+0

http://jsfiddle.net/WCPue/15/這是我從我的項目中得到的例子,如果你可以看一下並幫我從列表中刪除元素,框架是1.8 –

+0

@OfirAttia如果你正在使用jQuery 1.8那你爲什麼選擇jsfiddle 1.6?另外,你怎麼真的想刪除項目? –

+0

通過點擊刪除,我寫了框架是1.8 –

0

刪除多個選擇列表see this Edit

兩個條件下工作。

$("#theCheckbox").change(function() { 
    $("#theSelect").attr("multiple", (this.checked) ? "multiple" : ""); 
}).change(); 

$('a').click(function() { 
    $('#theSelect option:selected').remove(); 
}); 
+0

看看這個:http://jsfiddle.net/WCPue/22/有一個選項來限制點擊次數?避免重複。還有一件事,如果你可以看到它的多個列表,但只有2行可以在那裏。 –

0
$('#theSelect').change(function(){ 
    var selectedIndex = $(this)[0].selectedIndex; 
    //alert(selectedIndex); 
    var selected = $(this).children("option").eq(selectedIndex); 
    selected.remove(); 
}); 
$("#theCheckbox").change(function() { 
    $("#theSelect").attr("multiple", (this.checked) ? "multiple" : ""); 
}).change();​