2012-12-12 32 views
-1

我有一个div元素内的复选框。我想选中复选框作为托运whenevever我按一下按钮.. DIV元素标签如下jquery选择div元素内的复选框

<div class ="test123" style ="width:90px;"> 
<input id = "chk1" type = "checkbox" value="on"/> 
</div> 

我做这样的事情,但它不是为我工作

$(".test123 ").find("input:checked").each(function() { 
    //code to check the checkbox 
});​ 

请建议..

导航。

回答

0

您可以通过

$('#btn').click(function(){ 
$('#chk1').attr("checked","true"); 
}) 

<input type="button" id="btn" /> 
+0

但这意味着,我需要点击div,对不对?我想要的是,点击按钮,并在格子里面的复选框.. – Gagan

1
$('.btn').click(function(){ 
$('#chk1').attr('checked','checked'); // $('#chk1').attr("checked","true"); 
}) 

做到这一点都应该可以正常工作适合你。

HTML

<div class ="test123" style ="width:90px;"> 
<input id = "chk1" type = "checkbox" value="on"/> 
</div> 
<input type="button" class="btn" /> 
+0

但这将意味着,我需要点击div,对不对?我想要的是,点击按钮,并在格子里面的复选框。 – Gagan

+0

检查更新的答案 – Techie

+0

'class =“。btn”'? :) – Morpheus

0
$(".test123 ").find("input:checked").each(function() { 

代替上述之一..尝试使用

$("#button").click(function() { 
     $(".test123").find("input:checkbox").each(function() { 
      if($(this).attr('checked')) { 
      } else { 
       $(this).attr('checked','checked'); 
      } 
     }); 
    }); 
0
$('#btn').click(function(){ 

    $('.test123').find('input[type=checkbox]').attr('checked','true'); 



    }); 
1

您的解决方案的最佳方式是使用标签,而不是一个div像这样

<label class ="test123" style ="width:90px;"> 
    <input id="chk1" type="checkbox" value="on"/> 
</label> 

所以你不需要JavaScript代码!