how to select and deselect all options in a select box

梦想与她 提交于 2021-02-18 05:12:34

问题


<select name="remaintextarea" id="studentremain" size="10">
    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
</select>

<button type='button' id='selectall'>Select All</button>
<button type='button' id='deselectall'>De-Select All</button>

Got a select box above, select all and de-select all buttons above. My question is by using jquery, how can I get the button to select all the options and deselect all the options in the select box above when the relevant buttons are clicked on?

Below I just have the click handlers for both buttons:

$('#selectall').click(function() {

});   

$('#deselectall').click(function() {

});

回答1:


You need to add the multiple attribute to the select element, and then you can:

$('#selectall').click(function() {
    $('select#studentremain option').attr("selected","selected");
});   

$('#deselectall').click(function() {
    $('select#studentremain option').removeAttr("selected");
});



回答2:


Try this demo

Must add multiple="multiple" in select element

HTML:

<select multiple="multiple" name="remaintextarea" id="studentremain" size="10">
    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
</select>
<button type='button' id='selectall'>Select All</button>
<button type='button' id='deselectall'>De-Select All</button>

JS:

$('#selectall').click(function() {
    $('#studentremain > option').attr("selected", "selected");
});   

$('#deselectall').click(function() {
    $('#studentremain > option').removeAttr("selected");
});



回答3:


Somehow the answers above did not work for me. Seems that you have to use properties instead of attributes. Here is the code:

$('#selectall').click(function() {
    $('select#studentremain option').prop("selected",true);
});   

$('#deselectall').click(function() {
    $('select#studentremain option').prop("selected",false);
});

Documentation says that selected attribute only specifies that an option should be pre-selected when the page loads. But is not set when the actual option is selected by the user. Source




回答4:


Add the multiple attribute to your select box first so that you can have multiple values selected and then something like this would be sufficient since you're using jQuery.

//for selection of all.
$('#studentremain option').attr('selected','selected');

//for removal of selection.
$('#studentremain option').removeAttr('selected');

You can try prop and removeProp in the place of attr.




回答5:


Most of the answer were outdated, Here selecting and deselecting in two different ways

$('#selectall').click(function() {
    $("#studentremain option").prop("selected", true);
});   

$('#deselectall').click(function() {
    $("#studentremain option").val([]);
});


来源:https://stackoverflow.com/questions/14260428/how-to-select-and-deselect-all-options-in-a-select-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!