如何对未选中的复选框进行serializeArray? [英] How can I serializeArray for unchecked checkboxes?

查看:96
本文介绍了如何对未选中的复选框进行serializeArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何 修改此示例 ,以便从非复选框中获取值检查?

How can I modify this example so it can get values from checkboxes that aren't checked?

我希望所有复选框都有一个值,如果尚未检查我想要将其值设为 false

I want all checkboxes to have a value, if it hasn't been checked I want to get its value as false.

<input type="checkbox" name="Check01" value="true" />
<input type="checkbox" name="Check02" value="true" checked="checked" />

默认行为

$("form").serializeArray();
// [Check02 = true]

预期行为

$("form").serializeArray();
// [Check01 = false, Check02 = true]


推荐答案

最简单的方法就是自己动手:

It's probably easiest to just do it yourself:

 var serialized = $('input:checkbox').map(function() {
   return { name: this.name, value: this.checked ? this.value : "false" };
 });

如果还有其他输入,那么您可以序列化表单,然后找到未选中的复选框如上所述,并将结果附加到第一个数组。

If there are other inputs, then you could serialize the form, and then find the unchecked checkboxes with something like the above and append that result to the first array.

这篇关于如何对未选中的复选框进行serializeArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆