如何获得所有复选框的值

时间:2015-12-06 14:05:46

标签: javascript

我想获取所有复选框的值并按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}}

1 个答案:

答案 0 :(得分: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>
&#13;
&#13;
&#13;