如何选择和取消选择选择框中的所有选项 [英] how to select and deselect all options in a select box

查看:50
本文介绍了如何选择和取消选择选择框中的所有选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>

选中上方的选择框,全选,然后取消选择上方的所有按钮.我的问题是通过使用jquery,当单击相关按钮时,如何获取按钮以选择所有选项并取消选择上面选择框中的所有选项?

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() {

});

推荐答案

您需要将 multiple 属性添加到select元素,然后您可以:

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");
});

这篇关于如何选择和取消选择选择框中的所有选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆