如何使用JSON定义创建一个html表单? [英] how to create a html form using a JSON definition?
问题描述
我正在寻找一个JavaScript库,使我能够以JSON格式存储(html)表单,并将一些智能内置到验证输入客户端。一些东西:
I am looking for a javascript lib which enables me to store (html) forms in JSON format, with a bit of intelligence built into validate inputs client side. Something like:
function FormController(){
this.user = {
name: 'John Smith',
address:{line1: '123 Main St.', city:'Anytown', state:'AA', zip:'12345'},
contacts:[{type:'phone', value:'1(234) 555-1212'}]
};
this.state = /^\w\w$/;
this.zip = /^\d\d\d\d\d$/;
}
(这是从 http://docs.angularjs.org/#!cookbook.form )。这几乎是我想要的,因为它提供了基本的客户端验证与正则表达式,您可以提供默认值,但似乎仍然需要您创建HTML表单。
(This is taken from http://docs.angularjs.org/#!cookbook.form ). It's almost what I want, as it offers basic client side validation with regular expressions and you can provide defaults but it appears to still need you to create the HTML form.
我想要只需要定义的东西。任何想法?
I want something that only requires the definition. Any ideas?
推荐答案
在无耻的自我宣传中,我还想提及我的 jQuery.dForm插件。现在已经有一段时间了,支持jQuery验证插件,jQuery UI,并且易于扩展。这是它的外观:
In shameless self promotion I would also like to mention my jQuery.dForm plugin. It's been around for a while now and support the jQuery validation plugin, jQuery UI and is easily extensible. This is how it looks:
var formdata =
{
"action" : "index.html",
"method" : "get",
"elements" :
[
{
"name" : "textfield",
"caption" : "Label for textfield",
"type" : "text",
"value" : "Hello world"
},
{
"type" : "submit",
"value" : "Submit"
}
]
};
$("#myform").buildForm(formdata);
// Or to load the form definition via AJAX
$("#myform").buildForm("http://example.com/myform.json");
这篇关于如何使用JSON定义创建一个html表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!