admin管理员组文章数量:1400037
I am making a html form which includes a multiple select box. What i want to do is to make a "select all" option in multiple select box so that if the user user click on that option all the options of select box will automatically gets selected if user deselect it all other option gets deselected. any ideas using jQuery?
<select MULTIPLE>
<option>Select All</option>
<option>value1</option>
<option>value2</option>
<option>value3</option>
<option>value4</option>
<option>value5</option>
<option>value6</option>
</select>
I am making a html form which includes a multiple select box. What i want to do is to make a "select all" option in multiple select box so that if the user user click on that option all the options of select box will automatically gets selected if user deselect it all other option gets deselected. any ideas using jQuery?
<select MULTIPLE>
<option>Select All</option>
<option>value1</option>
<option>value2</option>
<option>value3</option>
<option>value4</option>
<option>value5</option>
<option>value6</option>
</select>
Share
Improve this question
edited Jan 27, 2017 at 10:07
Mohammad
21.5k16 gold badges56 silver badges84 bronze badges
asked Jan 27, 2017 at 8:32
brhn.lokbrhn.lok
531 gold badge1 silver badge8 bronze badges
1
- Wele to Stack Overflow, please take some time to follow the Stack Overflow tour and read about How do I ask a good question? Also provide us the relevant code you wrote for your question (You can also read How to create a Minimal, Complete, and Verifiable example) – Dipiks Commented Jan 27, 2017 at 8:34
2 Answers
Reset to default 3I store status (selected/unselected) of options in data-selected
attribute on click event. Then use it to select/unselect all option of listbox.
$("select").on("click", function(){
if ($(this).find(":selected").text() == "Select All"){
if ($(this).attr("data-select") == "false")
$(this).attr("data-select", "true").find("option").prop("selected", true);
else
$(this).attr("data-select", "false").find("option").prop("selected", false);
}
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select multiple data-select="false">>
<option>Select All</option>
<option>value1</option>
<option>value2</option>
<option>value3</option>
<option>value4</option>
<option>value5</option>
<option>value6</option>
</select>
You can do something like this using jQuery:
$('#selectAll').click(function() {
$('#persons option').prop('selected', true);
});
$('#unselectAll').click(function() {
$('#persons option').prop('selected', false);
});
input[type=button] {
width: 100px;
margin: 10px;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="selectAll" name="selectAll" value="Select All"><br />
<input type="button" id="unselectAll" name="unselectAll" value="Unselect All"><br /><br />
<select name="persons" id="persons" MULTIPLE size="8">
<option value="FR">Franck</option>
<option value="GE">Georges</option>
<option value="DA">David</option>
<option value="LU">Luc</option>
<option value="DO">Donald</option>
<option value="FR">FRANCOIS</option>
</select>
版权声明:本文标题:javascript - How to make a "select all" option in html multiple select box using jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744130127a2592139.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论