Disabled fields not picked up by serializeArray

前端 未结 4 937
伪装坚强ぢ
伪装坚强ぢ 2020-12-17 09:20

(Question updated to reflect real issue)

I just realized that serializeArray is not fetching content from disabled fields.

A set of

相关标签:
4条回答
  • 2020-12-17 09:34

    you can use readonly serializeArray() can read it.

    0 讨论(0)
  • 2020-12-17 09:35
    $('#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.

    0 讨论(0)
  • 2020-12-17 09:37

    This is expected behavior and won't be fixed. Here is a workaround

    function serializeJSONIncludingDisabledFields (form) {
      var fields = form.find('[disabled]');
      fields.prop('disabled', false);
      var json = form.serializeJSON();
      fields.prop('disabled', true);
      return json;
    }
    

    Link to the issue

    0 讨论(0)
  • 2020-12-17 09:50

    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');
    
    0 讨论(0)
提交回复
热议问题