我有这么多复选框:
<input type="checkBox" id="mango" value="mango" /> <label>MANGO</label><br> <input type="checkBox" id="santol" value="santol" /> <label>SANTOL</label><br> <input type="checkBox" id="guava" value="guava" /> <label>GUAVA</label><br> <input type="checkBox" id="lomboy" value="lomboy" /> <label>LOMBOY</label><br> <input type="checkBox" id="apple" value="apple" /> <label>APPLE</label><br> <input type="checkBox" id="orange" value="orange" /> <label>ORANGE</label><br> ...................<>
现在,我想要做的是通过检查获取所有的复选框的ID,并且不检查并放入数组。这样的事情
"fruitsGranted":["apple","lomboy","orange"]; //check is true "fruitsDenied":["mango","santol","guava"]; //check false
可以请有人告诉我怎么做谢谢。
解决方法
var someObj={}; someObj.fruitsGranted=[]; someObj.fruitsDenied=[]; $("input:checkBox").each(function(){ var $this = $(this); if($this.is(":checked")){ someObj.fruitsGranted.push($this.attr("id")); }else{ someObj.fruitsDenied.push($this.attr("id")); } });