检查复选框是否被选中 jquery
有谁知道为什么这不起作用。它总是打印“未选中”。
<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>
Does anyone know why this isn't working. It will always print 'unchecked'.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将
this.id
发送到该函数,然后将其包装在$()
中。这是行不通的。您只需将this
发送到 msg:onClick="msg(this)"
。You are sending
this.id
to the function, then you are wrapping it in the$()
. This does not work. You need just to sendthis
to msg:onClick="msg(this)"
.使用
Use
你需要一个 # 来告诉 jQuery 你想要选择一个 id X 而不是一个元素 < a href="http://api.jquery.com/element-selector/" rel="nofollow">X。
或者,如果您将
onClick="msg(this.id)"
更改为onClick="msg(this)"
您可以使用:You need a # to tell jQuery that you want to select an id X rather than an element X.
Or, if you change
onClick="msg(this.id)"
toonClick="msg(this)"
you can use: