检查是否选中了复选框jquery

Ray*_*ess 0 checkbox jquery checked

有谁知道为什么这不起作用.它将始终打印'未选中'.

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>

<script type="text/javascript">

function msg(x)
{

if($(x).attr('checked')){
  alert('checked!!');
}
else{
  alert('unchecked');
}

}

</script>


</head>
<body>

<form>
<input type="checkbox" id="fname2" onClick="msg(this.id)" /><br/>

</form>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Roc*_*mat 6

您正在发送this.id到该功能,然后您将其包装在$().这不起作用.你只需要发送this到msg : onClick="msg(this)".