Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jquery - Disabled fields not picked up by serializeArray

(Question updated to reflect real issue)

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

A set of (street) address fields are populated by selecting an item from a autosuggest list. 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.

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

Solution

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

(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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...