代码之家  ›  专栏  ›  技术社区  ›  RayLoveless

检查是否选中了复选框jquery

  •  0
  • RayLoveless  · 技术社区  · 14 年前

    有人知道为什么这不管用吗。它将始终打印“未选中”。

    <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>
    
    3 回复  |  直到 14 年前
        1
  •  6
  •   gen_Eric    14 年前

    你正在发送 this.id 对于函数,则将其包装为 $() . 这不起作用。你只需要发送 this 收件人消息: onClick="msg(this)" .

        2
  •  2
  •   Nikola K. geekman    12 年前

    使用

    function chkChecked(elmt) {
        if($(elmt).is(':checked')) {
            alert('checked!!');
        }
        else alert('unchecked');
    }
    
        3
  •  1
  •   WolfRevoKcats    14 年前

    你需要一个 # 告诉jQuery要选择id X而不是元素 X .

    if($('#'+x).attr('checked'))
    

    或者,如果你改变 onClick="msg(this.id)" onClick="msg(this)" 您可以使用:

    if($(x).attr('checked'))