jQuery serializeArray 未选取的禁用字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15958671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:06:36  来源:igfitidea点击:

Disabled fields not picked up by serializeArray

jqueryserialization

提问by Steven

(Question updated to reflect real issue)

(问题已更新以反映实际问题)

I just realized that serializeArrayis not fetching content from disabled fields.

我刚刚意识到这serializeArray不是从禁用字段中获取内容。

A set of (street) address fields are populated by selecting an item from a autosuggestlist. Once this is done, the fields are disabled. I could change this to read only, but I want the disabled look and feel without having to change CSS.

通过从autosuggest列表中选择一个项目来填充一组(街道)地址字段。完成此操作后,这些字段将被禁用。我可以将其更改为read only,但我想要禁用的外观和感觉而不必更改 CSS。

Is there a way to have serializeArraygrab data fro, the disabled fields?

有没有办法serializeArray从禁用的字段中获取数据?

Solution

解决方案

Thanks to Mohammad, I created a small plugin that helps me solve my issue:

感谢 Mohammad,我创建了一个小插件来帮助我解决我的问题:

(Fiddle)

小提琴

    var form_data = $('form').serializeAll();

    (function ($) {
      $.fn.serializeAll = function () {
        var data = $(this).serializeArray();

        $(':disabled[name]', this).each(function () { 
            data.push({ name: this.name, value: $(this).val() });
        });

        return data;
      }
    })(jQuery);

回答by Mohammad Adil

Try this

尝试这个

var data = $('form').serializeAllArray();

And here is the small plugin that is used

这是使用的小插件

(function ($) {
  $.fn.serializeAllArray = function () {
    var obj = {};

    $('input',this).each(function () { 
        obj[this.name] = $(this).val(); 
    });
    return $.param(obj);
  }
})(jQuery);

You can also try enabling all your element's just to serialize them and then disable them after serializing.

您还可以尝试启用所有元素来序列化它们,然后在序列化后禁用它们。

var myform = $('#form');
var disabled = myform.find(':input:disabled').removeAttr('disabled');
var serialized = myform.serializeArray();
disabled.attr('disabled','disabled');

回答by user6497164

you can use readonlyserializeArray() can read it.

您可以使用readonlyserializeArray() 可以读取它。

回答by Smith

$('#field').removeAttr('disabled');
var formData = $(this).serializeArray();
$('#field').attr('disabled', 'disabled');

The solution above is better - alternatively you can just remove the disabled attribute from the element, serialize the fields then re-add the attribute.

上面的解决方案更好 - 或者您可以从元素中删除 disabled 属性,序列化字段然后重新添加属性。