[英]How to clear all selected items in a SELECT input using jQuery?
I am think this should be fairly easy, but struggling a bit ... I have a SELECT
input element that allows the user to select multiple items.我认为这应该相当容易,但有点挣扎......我有一个
SELECT
输入元素,允许用户选择多个项目。 I would like to provide a mechanism for them to clear all selected items, probably will just be a SPAN
tag or something similar.我想为他们提供一种机制来清除所有选定的项目,可能只是一个
SPAN
标签或类似的东西。
Anyway ... using jQuery, how I would I clear the selection on the SELECT
input element so no items are selected.无论如何......使用jQuery,我将如何清除
SELECT
输入元素上的SELECT
以便没有选择任何项目。
This worked to clear all selected options for me..这有助于为我清除所有选定的选项。
$("#selectListName").prop('selectedIndex', -1)
The select list looked like选择列表看起来像
<select multiple='multiple' id='selectListName'>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
Try: // Just put # before select to fix this.尝试: // 只需在选择之前放置 # 即可解决此问题。 Works perfect.
工作完美。 $("#select option:selected").each(function () { $(this).remove(); //or whatever else });
$("#select option:selected").each(function () { $(this).remove(); // 或其他任何 });
只有这对我有用:-
$("#selectid").find('option').attr("selected",false) ;
This solution worked for me...这个解决方案对我有用......
$('#my-Select').val('').trigger('change');
This works on almost any input type.这适用于几乎所有输入类型。
我尝试类似的东西
$("#my-Select").find('option').attr("selected","") ;
Try this尝试这个
<select id='select1' ></select>
<input type= 'button' value = 'Clear Selection' id = 'remove' />
$(document).ready(function() {
$('#remove').click(function() {
return $('#select1 option:selected').remove();
});
});
Maybe
$('#mySelect option').each(function(i, e)
{
e.selected = false
});
Try that:试试看:
$("#selectID").empty();
Worked for me!为我工作!
Better than this, you can use:比这更好,您可以使用:
$("#selectID").empty();
That works on almost anything in the DOM.这几乎适用于 DOM 中的任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.