简体   繁体   English

如果使用Jquery SerializeArray()为空,如何序列化复选框,单选和多个选择元素?

[英]How Can I Serialize the checkbox, radio and multiple select elements if they are blank using Jquery SerializeArray()?

I've read the jquery documentation and it seems like input elements like checkbox, radio and multiple selects will not be serialized if they are blank. 我已经阅读了jquery文档,似乎输入元素(如复选框,单选和多选)如果为空,将不会被序列化。 I've also read this thread. 我也读过这个话题。 jQuery serialize does not register checkboxes which has given me quite good information about the problem. jQuery序列化不注册复选框 ,这给了我关于该问题的很好的信息。

But what I want here is, "Is there any elegant solution to this problem?", I just want a blank value if those elements are left blank. 但是,我想在这里说的是“该问题是否有任何优雅的解决方案?”,如果这些元素留为空白,我只想要一个空白值。

Any suggestions would be great. 任何建议都很好。

On the same example this is also mention. 在同一示例中,也提到了这一点。 Try this: 尝试这个:

/* Get input values from form */
values = jQuery("#myform").serializeArray();

/* Because serializeArray() ignores unset checkboxes and radio buttons: */
values = values.concat(
        jQuery('#myform input[type=checkbox]:not(:checked)').map(
                function() {
                    return {"name": this.name, "value": 'off'}
                }).get()
);

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

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