从序列化读取到填充表单
我在表单上执行了 serialize()
并保存了字符串,是否有任何函数可以将序列化字符串中的值填充回表单?
I did serialize()
on my form and saved the string, is there any function which can populate values back to the form from serialized string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是爆炸丸答案的更新版本,并应用了评论中的附加建议:
Here is the updated version of Explosion Pills' answer with the additional suggestions in the comments applied:
我建议查看 http://phpjs.org/functions/unserialize:571
而不是序列化数据为了与 JavaScript 进行通信,您可以使用
JSON
。 PHP 应该有json_encode()
和json_decode()
来帮助解决这个问题,而 javascript 也有内置的JSON
处理函数,但你可能没有甚至需要。例如,如果$.getJSON
从服务器获取有效的JSON字符串,它将自动转换为javascript对象。编辑:假设您正在谈论 jQuery 的
$.serialize()
,我知道没有函数可以撤消此操作(我什至不确定为什么需要这样做..)但这应该工作:Check out http://phpjs.org/functions/unserialize:571
I recommend instead of serializing data for communication with javascript, you use
JSON
. PHP should havejson_encode()
andjson_decode()
to help with this, and javascript also has built inJSON
handling functions, which you may not even need. For example, if$.getJSON
gets a valid JSON string from the server, it will be transformed into a javascript object automatically.EDIT: assuming you are talking about jQuery's
$.serialize()
, that I know of there's no function to undo this (I'm not even sure why that would ever be necessary..) but this should work:根据之前的答案,如果您有复选框或单选按钮:
Based on previous answers, if you have checkbox or radio buttons: