2015-12-06 78 views
-1

的所有價值我想獲得checkboxs的全部價值,並通過ID將它們存儲:如何獲得複選框

   var jsonObj = $("[type='checkbox']").map(function(i, o) { 
        return { id : o.id, checked : o.checked }; 
       }); 
       var checkboxs = ''; 
       if(jsonObj != 0){ 
        checkboxs = JSON.stringify(jsonObj); 
       }else{ 
        checkboxs = '' ; 
       } 

checkboxs變量輸出:對www.json.parser (複製/粘貼輸出。 online.fr)

{"0":{"id":"1","checked":false},"1":{"id":"2","checked":false},"2":{"id":"3","checked":false},"3":{"id":"4","checked":true},"4":{"id":"5","checked":true},"length":5,"prevObject":{"0":{"jQuery18305884705366101428":23},"1":{"jQuery18305884705366101428":31},"2":{"jQuery18305884705366101428":39},"3":{"jQuery18305884705366101428":47},"4":{"jQuery18305884705366101428":55},"length":5,"prevObject":{"0":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite.../subs/14988","search":"","hash":""},"jQuery18305884705366101428":1},"length":1},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1},"selector":"[type='checkbox']"},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1}} 

但它返回一些值,我根本不需要它們!

"length":5,"prevObject":{"0":{"jQuery18305884705366101428":23},"1":{"jQuery18305884705366101428":31},"2":{"jQuery18305884705366101428":39},"3":{"jQuery18305884705366101428":47},"4":{"jQuery18305884705366101428":55},"length":5,"prevObject":{"0":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite.../subs/14988","search":"","hash":""},"jQuery18305884705366101428":1},"length":1},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1},"selector":"[type='checkbox']"},"context":{"location":{"href":"mysite...","origin":"mysite...","protocol":"http:","username":"","password":"","host":"mysite...","hostname":"mysite...","port":"","pathname":"mysite...","search":"","hash":""},"jQuery18305884705366101428":1}} 
+0

你使用的地圖,我不認爲正確http://api.jquery.com/jquery.map/ –

回答

2

讓我以一步一步的方式解釋整個工作程序。請參閱評論在下面的代碼片段:

$(function() { 
 
    // Step 1: Create a final object, which stores all the values. 
 
    var allChk = {}; 
 
    // Step 2: Loop through all the checkboxes. 
 
    $("input:checkbox").each(function() { 
 
    // Step 3: For every checkbox you have, add the index as the id of the checkbox using this.id and give the value, whether it is checked or not using this.checked. 
 
    allChk[this.id] = this.checked; 
 
    }); 
 
    // You have filled the allChk object with the necessary information you need. 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<form id="myform"> 
 
    <input type="checkbox" name="one" id="one" /> 
 
    <input type="checkbox" name="two" id="two" /> 
 
    <input type="checkbox" name="three" id="three" /> 
 
</form>

+1

這不是完全可以解決問題所要求的。他顯然希望將ID和檢查的布爾標誌存儲爲JSON。 – ndugger

+0

@ndugger好的,會更新。請稍候。 –

+0

@ndugger請找到最新的答案。 –