2012-09-13 191 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();​