使用jQuery选中所有复选框并选择选项

Select all checkboxes using jQuery and select option

本文关键字:复选框 选择 选项 jQuery 使用      更新时间:2023-09-26

我有以下代码:

<select id="deloptions">
<option value="none">Select none</option>
<option value="all">Select all</option>
</select>

我正在尝试从这个选择之后的表中选中所有的复选框并取消选中所有复选框。复选框位于表格的第一列。他们都上"德尔姆"课。我什么都试过了,但似乎不起作用。。。这是我当前的代码:

$('#deloptions').change(function(){ 
var value = $(this).val();
if (value == 'all') {
    $('input:checkbox').prop('checked', true);
    } else {
    $('input:checkbox').prop('checked', false);
    }
});

我也试过用他们的课,但没有成功。。。类似:

$('.delme').prop('checked', false);

伸手去够桌子也没用。。。所以我被卡住了。

编辑:

<table>
<tr>
<th></th>
<th>Title</th>
<th>Content</th>
<th>Date Added</th>
<th>Actions</th>
</tr>
<tr id="37">
<td><input type="checkbox" class="delme" id="37"></td>
<td>Good news!</td>
<td>This is a message!</td>
<td>2013-07-22</td>
<td>Here is a button</td>
</tr>
</table>

尝试:

$('#deloptions').change(function(){ 
 var value = $(this).val();
 if (value == 'all') {
   $('input').attr('checked', 'checked');
 } else {
   $('input:checkbox').prop('checked', 'no');
 }
});

试试这个

$('input[type="checkbox"]').prop("checked", false);

试试这个

 $(function(){
    $("#deloptions").change(function(){
       if($("#deloptions  option:selected").val()=="all")
       {
              $('input[type="checkbox"]').prop("checked", false);
       }
       else
       {
              $('input[type="checkbox"]').prop("checked", true);
       }
    });
 })

您可能没有添加jQuery引用,因为您的代码运行良好

jsFiddle

试试这个代码。

$(function(){
// add multiple select / deselect functionality
$("#selectall").click(function () {
      $('.case').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){
    if($(".case").length == $(".case:checked").length) {
        $("#selectall").attr("checked", "checked");
    } else {
        $("#selectall").removeAttr("checked");
    }
});
});