jQuery Storage and retrieval form state (with data)

后端 未结 3 922
囚心锁ツ
囚心锁ツ 2021-01-03 09:43

Is there any way to store form state in for example cookie and retrieval it ?
I checked serialize API but I have no idea how to retrieval serialized data on the form.

3条回答
  •  半阙折子戏
    2021-01-03 10:32

    I prefer .serialize() and parsing with .split() over .searializeArray() and JSON parsing.

    qs = $("#form_id").searialize()  // generates query string, e.g: "name=John&age=21"
    f = {}
    qs.split("&").map( p => p.split("=") ).forEach( ([k,v]) => f[k] = v )
    // f === {name: "John", age: "21"}
    

提交回复
热议问题