我使用jQuery.serialize来检索表单中的所有数据字段。
我的问题是,它不会检索未选中的复选框。
它包括以下内容:
<input type="checkbox" id="event_allDay" name="event_allDay" class="checkbox" checked="checked" />
但不是这个
<input type="checkbox" id="event_allDay" name="event_allDay" class="checkbox" />
如何获取未选中的复选框的“值”?
发布于 2011-08-18 22:03:53
为了在azatoth的天才答案的基础上,我在我的场景中稍微扩展了一下:
/* 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": false}
}).get()
);
发布于 2010-06-13 03:42:00
您不需要这样做,因为serialize是作为查询字符串使用的,在该上下文中,未选中表示它根本不在查询字符串中。
如果您确实希望获得未选中复选框的值,请使用:(未测试课程)
var arr_unchecked_values = $('input[type=checkbox]:not(:checked)').map(function(){return this.value}).get();
发布于 2010-06-13 03:49:12
如果要将未序列化的复选框添加到查询字符串中,请将以下内容添加到jquery提交函数中:
var moreinfo = '';
$('input[type=checkbox]').each(function() {
if (!this.checked) {
moreinfo += '&'+this.name+'=0';
}
});
https://stackoverflow.com/questions/3029870
复制相似问题