简体   繁体   English

有没有一个JavaScript(jquery)库,用于从cgi参数更新html表单控件?

[英]Is there a javascript (jquery) library for updating html form controls from cgi parameters?

Our site has some very complicated form controls that are submitted and then show up again on the results page in case users want to resubmit the form from the results page. 我们的网站上有一些非常复杂的表单控件,这些表单控件已提交,然后再次显示在结果页面上,以防用户想要从结果页面重新提交表单。

Currently, this requires a lot of if-then statements to preset the form controls to what was submitted. 当前,这需要大量的if-then语句来将表单控件预设为提交的内容。

I'm wondering if there is a library (preferably jquery) that can take the cgi parameters from the URL and then update the form to those values onready. 我想知道是否有一个库(最好是jquery)可以从URL中获取cgi参数,然后将表单更新为现成的值。 Or if there is a slicker way that I'm not seeing even better. 或者,如果有一种更流畅的方式,我看不到更好的选择。 We use perl as our programming language. 我们使用perl作为我们的编程语言。

Persisting form state isn't usually approached from the client (JS) side, but from the server side. 持久表单状态通常不是从客户端(JS)端获取的,而是从服务器端获取的。 There are a number of good modules on CPAN for dealing with web forms, including HTML::FormFu and Form::Sensible , although personally I prefer HTML::FormHandler . CPAN上有许多用于处理Web表单的好模块,包括HTML :: FormFuForm :: Sensible ,尽管我个人更喜欢HTML :: FormHandler With it, you can declare your fields and all of their validation rules, and the module will validate the form for you, organize any errors on a per-field basis, and help you to render all of the form controls (including any error messages). 使用它,您可以声明您的字段及其所有验证规则,模块将为您验证表单,在每个字段中组织所有错误,并帮助您呈现所有表单控件(包括任何错误消息) )。 You get your choice of control over rendering, from a totally automatic layout (nice for development), to manual layout with FormHandler's builtin widgets, to providing custom templates for the widgets, to doing all of the templating yourself and just using the form object as a repository of field values and messages. 您可以选择对渲染的控制权,从全自动布局(适合开发)到使用FormHandler的内置小部件进行手动布局,为小部件提供自定义模板,自己进行所有模板制作以及仅将form对象用作字段值和消息的存储库。

You can use this 你可以用这个

$('#myId').load('myCGI.py');

This 这个

Load data from the server and place the returned HTML into the matched element. 从服务器加载数据,并将返回的HTML放入匹配的元素。

See jquery load . 请参阅jquery load

http://www.reach1to1.com/sandbox/jquery/testform.html http://www.reach1to1.com/sandbox/jquery/testform.html

This plugin takes JSON data and updates the form as desired. 该插件采用JSON数据并根据需要更新表单。

This plug creates JSON from the cgi parameter string http://benalman.com/code/projects/jquery-bbq/examples/deparam/ 该插件从cgi参数字符串http://benalman.com/code/projects/jquery-bbq/examples/deparam/创建JSON。

Put them together and add a class to any forms you want to apply this to and here you go in a one-liner to run on doc ready. 将它们放在一起,并将类添加到要应用此表单的任何表单上,在这里您可以一站式运行以准备文档。

function form_presetFromQueryString(){
    $('form.deserialize').deserialize( $.deparam.querystring() );
}

This SO answer describes a little plugin that can either capture a form's state, or apply a serialized state to a form. 这样的答案描述了一个小插件,该插件可以捕获表单的状态,也可以将序列化的状态应用于表单。 Perhaps this could be part of the solution. 也许这可能是解决方案的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM