简体   繁体   English

jQuery-未“选中”的SerializeArray()复选框

[英]Jquery - SerializeArray() checkbox that is not “checked”

so I am trying to implement the Jquery .serializeArray() method to transform a form into a JSON string to send it out in a request. 所以我正在尝试实现Jquery .serializeArray()方法,以将表单转换为JSON字符串以在请求中发送出去。 This works great for my form except for checkboxes and radio buttons. 除复选框和单选按钮外,这对我的表单非常有用。 The only time this works properly is when they are checked. 只有当检查它们时,此方法才能正常工作。 Sometimes, I need to know if they are unchecked. 有时,我需要知道它们是否未选中。 They still need to get serialized. 他们仍然需要序列化。

I suppose I could manually loop through the form and grab the values and build the JSON object, but that would not be optimal. 我想我可以手动遍历表单并获取值并构建JSON对象,但这并不是最佳选择。

According to the Jquery documentation found here: Jquery Docs anything that fits the W3 standards for a successful control found here should get included. 据这里找到jQuery的文档: jQuery的文档任何适合W3标准成功的控制在这里找到应该得到包括在内。 Unfortunately, this does not include checkboxes that are not checked. 不幸的是,这不包括未选中的复选框。 Does anyone have a work around for this behavior? 有没有人可以解决此问题? Thanks in advance... 提前致谢...


var form = document.getElementById('f');
console.log($(form).serializeArray();

That spits out the serialized form with the checkboxes that are not checked excluded... 这会弹出带有未选中复选框的序列化表格...

If you really want to use checkboxes and radio buttons, have those input fields update a corresponding hidden field. 如果您确实要使用复选框和单选按钮,请让这些输入字段更新相应的隐藏字段。 That way, the hidden input will always be sent. 这样,隐藏的输入将始终被发送。

how about trying this, I had a similar problem, I thought unchecked checkboxes should have a value as well, here is a quick work around, add an extra class on each checkbox on your form "cbx" make data an array from the form with serialise 怎么做呢,我遇到了类似的问题,我认为未选中的复选框也应该有一个值,这是一个快速的解决方法,在表单“ cbx”的每个复选框上添加一个额外的类,使数据来自表单连载

then loop through all checkboxes with a class of "cbx" and add them to the array with a value of 0, AFTER the array has been created with (serializeArray()) 然后遍历所有带有“ cbx”类的复选框,并将它们添加到值为0的数组中,然后使用(serializeArray())创建数组

when you post the data you will see the unchecked checboxes and values of 0 will get transferred with the post. 发布数据时,您会看到未选中的复选框,值0将随发布一起转移。

    var data = $('#form').serializeArray();

    $(".cbx:not(:checked)").each(function() {
        data.push({name: this.name, value: '0' });
    });

    $.post("testpage.asp", data);

An alternative would be to use a jQuery plugin. 一种替代方法是使用jQuery插件。 For example, serializeJSON has the option to set the value for unchecked checkboxes like this: 例如, serializeJSON可以选择为未选中的复选框设置值,如下所示:

$('form').serializeJSON({checkboxUncheckedValue: "false"});

In any case, it is usually best to use hidden inputs for unchecked values. 无论如何,通常最好将隐藏输入用于未经检查的值。

Alright, so I developed a workaround. 好的,所以我开发了一种解决方法。

Basically I wrote a function to compare the original JSON object to the serialized form. 基本上,我编写了一个将原始JSON对象与序列化形式进行比较的函数。 As I looped through, I compared the components. 当我遍历时,我比较了组件。 If there was a discrepancy, I pulled the component off the form and manually inserted it into the JSON. 如果存在差异,我将组件从表单中拉出,然后手动将其插入JSON。 I used the array.splice() method to add the missing components. 我使用array.splice()方法添加了缺少的组件。 Worked for all of the missing inputs. 为所有缺少的输入工作。

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

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