<input type="checkbox" value="第一" />第一<br/>
        <input type="checkbox" value="第二" />第二<br/>
        <input type="checkbox" value="第三" />第三<br/>
        <input type="checkbox" value="第四" />第四<br/>
        <input type="checkbox" value="第五" />第五<br/>
        <input type="button" value="提交" id="btn" />
$("#btn").click(function () {
            var values = "你是:";
            $("input[type='checkbox']").each(function (i) {
                alert($(this).attr("checked"));
            })
        })勾上之后怎么取到的都是undefined的呢?怎样能取到复选框的checked值jQuery

解决方案 »

  1.   

    $("input[type='checkbox']").each(function (i,item) {
                    alert($(item).attr("checked"));
                });这样试试,不过你那个看上去也是没有错的,怪事情,你自己调试调试,每次的元素的value是不是第一,第二,第三...
      

  2.   

    alert($(this).prop("checked"));在jQuery 1.6中,当属性没有被设置时候,.attr()方法将返回undefined。另外,.attr()不应该用在普通的对象,数组,窗口(window)或文件(document)上。若要检索和更改DOM属性请使用.prop()方法。 the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method
      

  3.   

    举个例子说,
    <div class="myclass" abc="my Attr">class 是DOM属性,而 abc 就不是 DOM 属性
    所以
    改变 class 要用 $("div").prop("class",[param])
    改变 abc 要用 $("div").attr("abc", [param])
      

  4.   

    $("#btn").click(function () {
                var values = "你是:";            
                $(":checkbox:checked").each(function (i) {
                 values+=this.value;                
                })
                alert(values);
            })
      

  5.   

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                $("#btn").click(function () {
                    var values = "你是:";
                    //遍历所有checkbox中选择的(如果你想全部都要,去掉:checked), 并显示相关信息
                    $(":checkbox:checked").each(function (i) {
                        //jQuery方式。 jQuery1.6以前用attr, 1.6及以后用prop
                        var result = "checked: " + $(this).prop("checked") + " , value: " + $(this).val();
                        //js方式。 建议这种有争议性的东西, 用原生的js方式。
                        result = "checked: " + this.checked + " , value: " + this.value; 
                        alert(result);
                    });
                })
            });
        </script>
    </head>
    <body>
        <input type="checkbox" value="第一" />第一<br />
        <input type="checkbox" value="第二" />第二<br />
        <input type="checkbox" value="第三" />第三<br />
        <input type="checkbox" value="第四" />第四<br />
        <input type="checkbox" value="第五" />第五<br />
        <input type="button" value="提交" id="btn" />
    </body>
    </html>jquery checkbox 全选/全不选 的通用写法 : 
    http://blog.csdn.net/yenange/article/details/8543772