jQuery返回所有选中的复选框,即使只有一个选中

时间:2022-03-13 09:49:23

As per this JSFIDDLE --

根据这个JSFIDDLE——。

I have been going crazy trying to figure this out for last 48 hours. Everything works except for the checkbox. I included the isolated snippet of code that is causing the issue in this.

在过去的48小时里,我一直在疯狂地想办法解决这个问题。除了复选框之外,一切都正常。我包含了导致此问题的独立代码片段。

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.

在我创建了复选框(按下复选框按钮)并完成表单之后,我成功地看到了代码,并能够将复选框序列化为JSON——但是,我最终得到了所有复选框的“on”,即使其中只有一个复选框被选中。

When I checked only one (first box) and other boxes unchecked, and clicked 'save', I get this as result:

当我只选中一个(第一个框)和其他未选中的框并单击“save”时,我得到的结果是:

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

{“t”:“在”,“ck2”:“在”,“ck3”:“在”,“submit1”:“拯救”}

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。

2 个解决方案

#1


1  

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:

如果您只想从复选框中获得数据,那么您可以从start开始筛选选中的复选框:

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

#2


0  

You're arbitrarily assigning the value of all inputs, you're not actually checking the checked state.

你在任意地分配所有输入的值,你实际上并没有检查被检查的状态。

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.

对于单选按钮,您也需要一个例外。

#1


1  

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:

如果您只想从复选框中获得数据,那么您可以从start开始筛选选中的复选框:

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

#2


0  

You're arbitrarily assigning the value of all inputs, you're not actually checking the checked state.

你在任意地分配所有输入的值,你实际上并没有检查被检查的状态。

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.

对于单选按钮,您也需要一个例外。