1. 程式人生 > >把checkbox選中的循環放到數組

把checkbox選中的循環放到數組

ack att .com ava func con src length cti

<html>
    <head>
        <meta charset="utf-8">
        
        <script src="http://oa.feeyo.com/js/jquery-1.4.2.min.js" type="text/javascript"></script>
    </head>

    <body>
    <div id=‘xxx‘>
        <input type="checkbox" value="橘子" id=‘id2‘ name="check"
>橘子1</input> <input type="checkbox" value="香蕉" id=‘id3‘ name="check">香蕉1</input> <input type="checkbox" value="西瓜" id=‘id4‘ name="check">西瓜1</input> <input type="checkbox" value="芒果" id=‘id5‘ name="check">芒果1</input> <input
type="checkbox" value="葡萄" id=‘id6‘ name="check">葡萄1</input> <input type="button" value="獲取" id="b1"> </div> </body> <script> //方法1 $("#b1").click(function(){ var data_arr =[]; //定義一個空數組 用於存放數組json //
獲取選中的多多選框 #xxx自己改 因為是多個多選框 結果就是一個數組 如果是 單選框或者是輸入框 就不是數組 var check_arr = $(#xxx input[type=checkbox]:checked); alert(check_arr.length); // 彈出看看有幾個 //然後循環他 $.each(check_arr,function(){ //想數組裏添加一個元素 data_arr.push( { id:$(this).attr(id), value:$(this).val() } ); }); console.log(data_arr); }); </script> </html>

把checkbox選中的循環放到數組