2013-07-22 60 views
0

所有复选框我有以下代码:选择使用jQuery和选择选项

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

我试图检查所有theck并取消所有从被放置在这之后选择一个表的复选框。复选框放在表格的第一列中。他们都有“delme”课。我已经竭尽所能,但似乎并没有工作......这是我当前的代码:

$('#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); 

试图达到表没有工作...所以我kindda斯图尔特。

编辑:

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

做工精细[这里](http://jsfiddle.net/7vxYA/) –

+0

有没有在你的JavaScript控制台的任何错误? – cfs

+0

代码与表也很好http://jsfiddle.net/ZKW6U/ –

回答

0

试试这个

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

明白了。问题是我已经导入了旧版本的jquery(1.3.0):D – paz

0

试试这个

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

明白了。问题是我已经导入了旧版本的jquery(1.3.0):D – paz

0

您可能没有添加jQuery引用作为你的代码是工作的罚款

jsFiddle

0

试试这段代码。

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

}); 
}); 
1

尝试:

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