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

javascript - Loading HTML controls from JSON in jQuery (that is, reverse of Ajax serialiseArray) - Stack Overflow

programmeradmin1浏览0评论

I would like to take a set of controls (INPUT, SELECT, TEXTAREA) which are contained within a DIV and send their values as JSON via Ajax to a server. This is easy enough with jQuery's serializeArray.

However I then want the server to respond with the same structure of JSON that was sent and re-load the control values using the provided JSON. I can't find anything in the jQuery documentation that would make this a simple operation.

Am I missing something or do I need to build this myself?

I would like to take a set of controls (INPUT, SELECT, TEXTAREA) which are contained within a DIV and send their values as JSON via Ajax to a server. This is easy enough with jQuery's serializeArray.

However I then want the server to respond with the same structure of JSON that was sent and re-load the control values using the provided JSON. I can't find anything in the jQuery documentation that would make this a simple operation.

Am I missing something or do I need to build this myself?

Share Improve this question edited Apr 1, 2011 at 21:10 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Dec 29, 2008 at 15:32 AnthonyWJonesAnthonyWJones 189k35 gold badges235 silver badges307 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

why not just have the server send back in the same: controlname:value structure and then read that out in jQuery and use something like:

$("*[name='" + controlname + "']").val( value);

Or even easier: controlID:value

$("#" + controlID).val( value);

Form controls are not bound to JSON data.

The JSON data could be intended for any purpose, so you'll have to build a function to take the JSON result and fiddle with the form elements yourself.

Forms are too plex to expect jQuery to handle this natively. In the past I've written frameworks to build forms from XML/JSON-based definitions and to handle the data transport using the same. It's not all that hard to do yourself, but it's not something built-in.

I took a quick peek through the plug-ins and didn't notice anything that specifically does that either, although it does sound like a good idea. It sounds more like something that would be supported though Ext.js.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论