来自json的jquery dform插件加载?

时间:2013-10-31 13:59:37

标签: jquery json forms

我正在使用JQuery dForm插件来创建表单。它适用于以下代码:

$("#myform").dform({
            "action" : "index.html",
            "method" : "get",
            "html" :
            [
                {
                    "type" : "p",
                    "html" : "You must login"
                },
                {
                    "name" : "username",
                    "id" : "txt-username",
                    "caption" : "Username",
                    "type" : "text",
                    "placeholder" : "E.g. user@example.com"
                },
                {
                    "name" : "password",
                    "caption" : "Password",
                    "type" : "password"
                },
                {
                    "type" : "submit",
                    "value" : "Login"
                }
            ]
        });

但是如果我将表单定义移动到JSON文件中,如:

"action" : "index.html",
            "method" : "get",
            "html" :
            [
                {
                    "type" : "p",
                    "html" : "You must login"
                },
                {
                    "name" : "username",
                    "id" : "txt-username",
                    "caption" : "Username",
                    "type" : "text",
                    "placeholder" : "E.g. user@example.com"
                },
                {
                    "name" : "password",
                    "caption" : "Password",
                    "type" : "password"
                },
                {
                    "type" : "submit",
                    "value" : "Login"
                }
            ]

尝试使用以下命令从json加载表单定义:

$("#myform").dform('test.json', function(data) {
          this //-> Generated $('#myform')
          data //-> data from path/to/form.json
        });

表单无法加载。我是否需要在JSon文件或代码中添加其他内容?

非常感谢任何帮助。

谢谢, 卡洛斯

1 个答案:

答案 0 :(得分:0)

这是JSon中的一个问题。基本上JSon DForm定义可以是:

{"id":"form","action":"","html" :[{"type" : "p","html" : "You must login"},{"name" : "username","id" : "txt-username","caption" : "Username","type" : "text","placeholder" : "E.g. user@example.com","onkeypress":"myKeypress"},{"name" : "password","caption" : "Password","type" : "password","onkeypress":"myKeypress"}]}

卡洛斯。