最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Opposite to serializeArray in jQuery (restore form) - Stack Overflow

programmeradmin3浏览0评论

Having the data from serializeArray, how do you update the form with it?

var values = form.serializeArray();
form.deserializeArray(value); // What is the deserializeArray analogue?
form.seriaizeArray() === values; // So that this is always true

Having the data from serializeArray, how do you update the form with it?

var values = form.serializeArray();
form.deserializeArray(value); // What is the deserializeArray analogue?
form.seriaizeArray() === values; // So that this is always true
Share Improve this question asked Feb 21, 2012 at 0:25 Dmytrii NagirniakDmytrii Nagirniak 24.1k13 gold badges77 silver badges132 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

See jQuery plugin to serialize a form and also restore/populate the form

We can iterate over the array and restore the form.

for (var i = 0; i < values.length; i++) {
    $("input[name='" + values[i].name + "'], select[name='" + values[i].name + "']").val(values[i].value);
}

This work for me

// By Jorhel Reyes
jQuery.fn.deserializeArray = function (ObjectSerialized, isJson) 
{
    var $form = jQuery(this);
    var json = {};
        jQuery.each(ObjectSerialized, function(i, pair){
            var name = decodeURIComponent(pair.name).split("[");
            if( typeof name[1] != "undefined" ){
                if( typeof json[name[0]] === "undefined"){ json[name[0]] = []; }
                json[name[0]].push(decodeURIComponent(pair.value));
            }else{
                json[name[0]] = decodeURIComponent(pair.value);
            }
        });
    var asignValue = function(element, val){
        if( !element.length ){ return; }
        if (element[0].type == "radio" || element[0].type == "checkbox") {
            var $fieldWithValue = element.filter('[value="' + val + '"]');
            var isFound = ($fieldWithValue.length > 0);
            // Special case if the value is not defined; value will be "on"
            if (!isFound && val == "on") {
                element.first().prop("checked", true);
            } else {
                $fieldWithValue.prop("checked", isFound);
            } 
        } else { // input, textarea
            element.val(val);
        }
    };
    jQuery.each(json, function(name, value){
        var element = '';
        if( typeof value === "object" ){
            element = $form.find('[name="' + name + '[]"]');
            jQuery.each(value, function(k, val){
                var elm = jQuery( element[k] );
                asignValue(elm, val);
            });
        }else{
            asignValue($form.find('[name="' + name + '"]'), value);
        }
    });
    return this;
}

The opposite to serializeArray is .param()

In fact, the serialize function does a serializeArray first, and then applies param()

serialize: function() {
    return jQuery.param( this.serializeArray() );
},
serializeArray: function() {
...
发布评论

评论列表(0)

  1. 暂无评论