简体   繁体   English

jQuery返回复选框为全部选中状态,即使只有一个选中

[英]jQuery returns checkboxes as all checked even if only one is checked

As per this JSFIDDLE -- 按照这个JSFIDDLE-

I have been going crazy trying to figure this out for last 48 hours. 在过去的48小时里,我一直在疯狂地试图解决这个问题。 Everything works except for the checkbox. 除复选框外,一切正常。 I included the isolated snippet of code that is causing the issue in this. 我在其中包含了导致问题的孤立代码段。

After I created boxes (pressing checkbox button) and finalize the form, I was successful in seeing code and was able to serialize checkboxes into JSON - but, I ended up getting 'on' for all checkboxes even though if only one of them is checked. 创建框(按复选框按钮)并完成表单后,我成功地看到了code并且能够将复选框序列化为JSON但是,即使只选中了其中一​​个复选框,我最终还是打开了所有复选框。

When I checked only one (first box) and other boxes unchecked, and clicked 'save', I get this as result: 当我仅选中一个(第一个框)而未选中其他框,然后单击“保存”时,我得到以下结果:

{"ck1":"on","ck2":"on","ck3":"on","submit1":"save"}

What was it that threw the result off? 是什么使结果无效? Am I doing something wrong in this code? 我在这段代码中做错了吗?

A help would be appreciated in identifying the issue. 在确定问题时将提供帮助。
My goal is to see JSON in this format when I have first checkbox checked: 我的目标是在选中第一个复选框时以这种格式查看JSON:

{"ck1":"on","submit1":"save"} or any format that you would suggest. {"ck1":"on","submit1":"save"}或您建议的任何格式。

EDITED: 编辑:

Find below the function I assinged to submit event: 在我希望提交的活动下面找到功能:

 $('#form'+formnum).submit( function(e) {
e.preventDefault(e);
var data = {};

//Gathering the Data
$.each(this.elements, function(i, v){
        var input = $(v);
        data[input.attr("id")] = input.val();
 //delete data[button.attr("id")]; <-- cant' figure it out
//removeData[submit] <-- cant' figure it out
}); // end of $.each

var output =JSON.stringify(data);
$('#showurl').text(output);
}); //end of 'save' button function

JSFIDDLE: jsfiddle . JSFIDDLE: jsfiddle

You are getting the value from the checkboxes, and the value is always the same regardless of the state of the checkbox. 您是从复选框中获取值的,并且无论复选框的状态如何,该值始终相同。

Check the type of the element to get the state for checkboxes: 检查元素的类型以获取复选框的状态:

if (input.is(':checkbox')) {
  if (input.is(':checked')) {
    data[input.attr("id")] = input.val();
  }
} else {
  data[input.attr("id")] = input.val();
}

If you only want data from the checkboxes, you can just filter out the the checked checkboxes from start: 如果只需要复选框中的数据,则可以从开始就过滤掉选中的复选框:

$(':checked', this).each(function(i, v){ data[v.id] = v.value; });

You're arbitrarily assigning the value of all inputs, you're not actually checking the checked state. 您是在任意分配所有输入的值,实际上并不是在checked状态。

Add a conditional line for: 为以下项添加条件行:

data[input.attr("id")] = input.val();

Something like: 就像是:

if (/* (input is a checkbox and checkbox is checked) or input is not a checkbox*/) {
        data[input.attr("id")] = input.val();
}

You also need an exception for radio buttons. 您还需要单选按钮的例外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM