jQuery deserialize form

后端 未结 11 684
日久生厌
日久生厌 2020-12-13 09:21

I am using jQuery Serialize to serialize my form elements and would like to deserialize them back. Unfortunately can\'t find any working jQuery deserializer, any suggestions

相关标签:
11条回答
  • 2020-12-13 09:25

    May be a bit late, but perhaps you are looking for something like JQuery.deserialize. Problems: no support for checkboxes or radio buttons.

    0 讨论(0)
  • 2020-12-13 09:25

    this code returns an array when same key is spotted multiple times in the serialized string

    
        chaine="single=Single1&multiple=Multiple&multiple=Multiple1&multiple=Multiple2&multiple=Multiple3&check=check2&radio=radio1"
        function deserialize(txt){
        myjson={}
        tabparams=chaine.split('&')
        var i=-1;
        while (tabparams[++i]){
        tabitems=tabparams[i].split('=')
        if( myjson[decodeURIComponent(tabitems[0])] !== undefined ){
            if( myjson[decodeURIComponent(tabitems[0])] instanceof Array ){
                myjson[decodeURIComponent(tabitems[0])].push(decodeURIComponent(tabitems[1]))
            }
        else{
           myjson[decodeURIComponent(tabitems[0])]= [myjson[decodeURIComponent(tabitems[0])],decodeURIComponent(tabitems[1])]
                }
            }
        else{
             myjson[decodeURIComponent(tabitems[0])]=decodeURIComponent(tabitems[1]);
            }
        }
        return myjson;
        }
    
    0 讨论(0)
  • 2020-12-13 09:33

    Bit late on this one, but somebody might find this useful.

    function fetchInput(identifier) {
        var form_data = identifier.serialize().split('&');
        var input     = {};
    
        $.each(form_data, function(key, value) {
            var data = value.split('=');
            input[data[0]] = decodeURIComponent(data[1]);
        });
    
        return input;
    }
    
    0 讨论(0)
  • 2020-12-13 09:40

    Try this:

    function deparam(query) {
        var pairs, i, keyValuePair, key, value, map = {};
        // remove leading question mark if its there
        if (query.slice(0, 1) === '?') {
            query = query.slice(1);
        }
        if (query !== '') {
            pairs = query.split('&');
            for (i = 0; i < pairs.length; i += 1) {
                keyValuePair = pairs[i].split('=');
                key = decodeURIComponent(keyValuePair[0]);
                value = (keyValuePair.length > 1) ? decodeURIComponent(keyValuePair[1]) : undefined;
                map[key] = value;
            }
        }
        return map;
    }
    
    0 讨论(0)
  • 2020-12-13 09:41

    If what you want is to remove the standard URL-encoded notation, you can use JavaScript's decodeURIComponent(), which will give you a regular string, just like this:

    var decodedString = decodeURIComponent("Http%3A%2F%2FHello%3AWorld");
    alert(decodedString);
    

    In this case, decodedString will look like Http://Hello:World, here's a working fiddle.

    Got all of this searching for this same issue, and found the answer here: How can I decode a URL with jQuery?

    I know this is an old question, but doing some searches for jQuery deserialize got me here, so I might as well try to give a different approach on the issue for people with the same problem.

    0 讨论(0)
  • 2020-12-13 09:42

    Needed all in a single string, which can be stored in maybe COOKIE, and later read and fill the same form with input values.

    Input elements seperator: ~ (use any seperator)

    Input attributes seperator: | (use any seperator)

    input type | input name | input value ~ input2 type | input2 name | input2 value

    var formData = '';
    $('#form_id').find('input, textarea, select').each(function(i, field) {
        formData += field.type+'|'+field.name+'|'+field.value+'~';
    });
    

    Example:

    hidden|vote_id|10~radio|option_id|~radio|option_id|427~radio|option_id|428~
    
    0 讨论(0)
提交回复
热议问题