在jQuery中包含空值字段.serialize() [英] Include empty value fields in jQuery .serialize()

查看:117
本文介绍了在jQuery中包含空值字段.serialize()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过jQuery $ .post提交表单并通过

I am trying to submit a form via jQuery $.post and serialize the form data via

$('form').serialize();

不幸的是,未选中单选按钮复选框的字段没有被序列化,ergo提交。

Unfortunately fields with unchecked radiobutton or checkboxes are not being serialized, ergo submitted.

是否可以包含所有字段,无论它们是否包含值?

Is there a way to include ALL fields regardless of whether they contain a value or not?

我想这只影响这样的字段

I guess this only affects fields like this

<input type="checkbox" name="some_name[]" value="1" />
<input type="checkbox" name="some_name[]" value="2" />


推荐答案

制作您自己的序列化版本:

Make your own version of serialize :

(function( $ ){
  $.fn.mySerialize = function() {
    var returning = '';
    $('input, textarea',this).each(function(){
          var name = this.name;
          var value = this.value;
          returning += name + '=' + value + '&';
    })
    return returning;

  };
})( jQuery );


$('form').mySerialize();

小提琴: http://jsfiddle.net/maniator/apGC3/

这篇关于在jQuery中包含空值字段.serialize()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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