jQuery 如何让 $.serialize() 考虑那些禁用的 :input 元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4748655/
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 17:52:45  来源:igfitidea点击:

How do I make $.serialize() take into account those disabled :input elements?

jqueryserialization

提问by fms

It seems by default disabled input elements are ignored by $.serialize(). Is there a workaround?

似乎默认禁用的输入元素被$.serialize(). 有解决方法吗?

回答by user113716

Temporarily enable them.

暂时启用它们。

var myform = $('#myform');

 // Find disabled inputs, and remove the "disabled" attribute
var disabled = myform.find(':input:disabled').removeAttr('disabled');

 // serialize the form
var serialized = myform.serialize();

 // re-disabled the set of inputs that you previously enabled
disabled.attr('disabled','disabled');

回答by Andrew

Use readonly inputs instead of disabled inputs:

使用只读输入而不是禁用输入:

<input name='hello_world' type='text' value='hello world' readonly />

This should get picked up by serialize().

这应该由 serialize() 获取。

回答by Krzysiek

You can use a proxied function (it affects both $.serializeArray()and $.serialize()):

您可以使用代理函数(它同时影响$.serializeArray()$.serialize()):

(function($){
    var proxy = $.fn.serializeArray;
    $.fn.serializeArray = function(){
        var inputs = this.find(':disabled');
        inputs.prop('disabled', false);
        var serialized = proxy.apply( this, arguments );
        inputs.prop('disabled', true);
        return serialized;
    };
})(jQuery);

回答by Aaron Hudon

@user113716 provided the core answer. My contribution here is just a jQuery nicety by adding a function to it:

@user113716 提供了核心答案。我在这里的贡献只是通过向它添加一个函数来实现 jQuery 的精妙之处:

/**
 * Alternative method to serialize a form with disabled inputs
 */
$.fn.serializeIncludeDisabled = function () {
    let disabled = this.find(":input:disabled").removeAttr("disabled");
    let serialized = this.serialize();
    disabled.attr("disabled", "disabled");
    return serialized;
};

Example usage:

用法示例:

$("form").serializeIncludeDisabled();

回答by KennyKam

Try this:

尝试这个:

<input type="checkbox" name="_key" value="value"  disabled="" />
<input type="hidden" name="key" value="value"/>

回答by David162795

I can see a few workarounds, but still no-one suggested writing your own serializing function? Here you go: https://jsfiddle.net/Lnag9kbc/

我可以看到一些解决方法,但仍然没有人建议编写自己的序列化函数?给你:https: //jsfiddle.net/Lnag9kbc/

var data = [];

// here, we will find all inputs (including textareas, selects etc)
// to find just disabled, add ":disabled" to find()
$("#myform").find(':input').each(function(){
    var name = $(this).attr('name');
    var val = $(this).val();
    //is name defined?
    if(typeof name !== typeof undefined && name !== false && typeof val !== typeof undefined)
    {
        //checkboxes needs to be checked:
        if( !$(this).is("input[type=checkbox]") || $(this).prop('checked'))
            data += (data==""?"":"&")+encodeURIComponent(name)+"="+encodeURIComponent(val);
    }
});

回答by Jason Lewis

Disabled input elements don't get serialized because 'disabled' means they shouldn't be used, per W3C standard. jQuery is just abiding by the standard, even though some browsers don't. You can work around this, by adding a hidden field with a value identical to the disabled field, or by doing this via jQuery, something like this:

根据 W3C 标准,禁用的输入元素不会被序列化,因为“禁用”意味着不应使用它们。jQuery 只是遵守标准,即使某些浏览器不遵守。您可以解决此问题,方法是添加一个值与禁用字段相同的隐藏字段,或者通过 jQuery 执行此操作,如下所示:

$('#myform').submit(function() {
  $(this).children('input[hiddeninputname]').val($(this).children('input:disabled').val());
  $.post($(this).attr('url'), $(this).serialize, null, 'html');
});

Obviously, if you had more than one disabled input, you'd have to iterate over matching selectors, etc.

显然,如果您有多个禁用的输入,则必须迭代匹配的选择器等。

回答by maximran

In case someone don't want to activate them, then disable them again, you can also try to do this (I modified it from Disabled fields not picked up by serializeArray, from using a plugin to using a normal function):

如果有人不想激活它们,然后再次禁用它们,您也可以尝试执行此操作(我将其从Disabled fields not pick up by serializeArray修改为使用插件到使用普通功能):

function getcomment(item)
{
  var data = $(item).serializeArray();
  $(':disabled[name]',item).each(function(){
    data.push({name: item.name,value: $(item).val()});
  });
  return data;
}

So you can call them like this:

所以你可以这样称呼它们:

getcomment("#formsp .disabledfield");

回答by Carl Verret

Just over Aaron Hudon :

就在 Aaron Hudon 上方:

Maybe you've got something else than Input (like select), so I changed

也许你有输入以外的东西(比如选择),所以我改变了

this.find(":input:disabled")

to

this.find(":disabled")