获取jQuery中的复选框值


Get check box value in jQuery?

我需要在jQuery中获得一个复选框值。这是我的代码:

$(document).ready(function() {
    $('input[type="checkbox"]').bind('click',function() {
        var tObj = $('#checkpermission');
        var tVal = tObj.val();
        alert(tVal);     
    });
});
$query="select * from tbl_user where username!='admin' and password!='admin' order by username limit  $eu, $limit";
$result=mysql_query($query);
while($data=mysql_fetch_array($result)) {
   <td><INPUT TYPE="checkbox" name="checkuser" id="checkpermission" onclick="changePage()" value='<?=$data[0]?>'></td>
  <?php
}

每次我只得到第一个值。我做错了什么?

您可以执行以下操作:

$(document).ready(function(){
   $('input[type="checkbox"]').bind('click',function() 
   {
     alert($(this).is(":checked"));
     //Or you could use alert($(this).attr("checked") == "checked");
   });
});

更新:添加了复选框以查看复选框是否已选中。

您只得到第一个值,因为您的复选框具有相同的id,并且您没有在javascript 中迭代复选框

与前面的注释相同,但您可以这样做来正确确定是否选中了特定的复选框:

var tVal=tObj.is(":checked");

希望能有所帮助!Neil