2015-12-06 79 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请找到最新的答案。 –