1. 程式人生 > >迴圈checkbox的列表,實時列印已經選中的checkbox

迴圈checkbox的列表,實時列印已經選中的checkbox

    <div>
        <input type="checkbox" abc="abc" value="cc1">cc1
        <input type="checkbox" abc="abc" value="cc2">cc2
        <input type="checkbox" abc="abc" value="cc3">cc3
        <input type="checkbox" abc="abc" value="cc4">cc4
        <input type="checkbox" abc="abc" value="cc5">cc5
        <input type="checkbox" abc="abc" value="cc6">cc6
        <input type="checkbox" abc="abc" value="cc7">cc7
        <input type="checkbox" abc="abc" value="cc8">cc8
        <input type="checkbox" abc="abc" value="cc9">cc9
        <input type="checkbox" abc="abc" value="cc10">cc10
    </div>
    <div>
        <h1>顯示器</h1>
        <p id="cc"></p>
    </div>

    var a=[],b;
    $("input[abc='abc']").click(function(){
        a.length = 0;
        $("input[abc='abc']").each( function () {
            if ( $(this).prop('checked') ){
                console.log( $(this).val() );
                a.push( $(this).val() );
                b = a.join(",");
                $( '#cc' ).text(b);
            }
        })
    })

    var result= {
        name:'yinxing',
        age:18,
        num_rows:1
    }