jQuery deserialize form

后端 未结 11 686
日久生厌
日久生厌 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

    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;
        }
    

提交回复
热议问题