JavaScript 从 JSON 或类似数据生成/呈现动态 HTML 表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5042086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:23:27  来源:igfitidea点击:

JavaScript to generate/render dynamic HTML form from JSON or similar data?

javascripthtmlforms

提问by none

I would like to offer viewers a contact form that is modified according to the user's input. An example of such a form is on the Ext JS site. I have not looked into the product, but I would like to know if there are any other programs/functions that generate such a form dynamically? I found only samples on adding other input elements to existing forms.

我想为观众提供一个根据用户输入修改的联系表格。Ext JS 站点上提供了此类表单的一个示例。我没有研究过该产品,但我想知道是否还有其他程序/函数可以动态生成这样的表单?我只找到了将其他输入元素添加到现有表单的示例。

回答by Jonny Buchanan

Here are some other implementations:

下面是一些其他的实现:

I also plan to add this functionality to my own js-forms library, which would also handle validation.

我还计划将此功能添加到我自己的 js-forms 库中,该库也将处理验证。

回答by Robert Koritnik

You can always create whatever DOM structure you like using JavaScript. Be it additional inputs or even additional FORMelements. It then depends of course what you use on the server side to process form data and how you do that.

您可以随时使用 JavaScript 创建您喜欢的任何 DOM 结构。无论是额外的输入还是额外的FORM元素。然后当然取决于您在服务器端使用什么来处理表单数据以及您如何执行此操作。

Maybe I haven't understood the nature of your problem because you haven't explained yourself too good.

也许我没有理解你问题的本质,因为你没有很好地解释自己。

ExtJS is a JavaScript library that does everything on the client side. It also provides the ability to rapidly create user interface by providing Javascript configuration objects that will result in rich widget creation.

ExtJS 是一个 JavaScript 库,可以在客户端执行所有操作。它还提供了通过提供 Javascript 配置对象来快速创建用户界面的能力,这些对象将导致丰富的小部件创建。

You can do lots with jQuery (and jQueryUI) as well, but it's up to you how to do it. ExtJS just has these very functionally rich and visually consistent set of client controls/widgets that you can use out of the box without much additional development (not to mention bug killing that comes along custom development).

您也可以使用 jQuery(和 jQueryUI)做很多事情,但这取决于您如何去做。ExtJS 只是拥有这些功能非常丰富且视觉上一致的客户端控件/小部件集,您可以开箱即用,无需太多额外开发(更不用说自定义开发带来的错误清除)。

For instance: When writing an Asp.net MVC application it's very easy to issue an Ajax call that would return HTML of so called partial view with complete HTML of your form that you can then easily display in a modal dialog for instance.

例如:在编写 Asp.net MVC 应用程序时,很容易发出 Ajax 调用,该调用将返回所谓的部分视图的 HTML,其中包含表单的完整 HTML,然后您可以轻松地在模式对话框中显示。

If you do need all that functionality to create a desktop-like application I suggest you do take a plunge into ExtJS because it is definitely a very good product. I've used it about 3 years ago (version 2) on some project and I was amazed by the work they've done with it. It's very feature rich product that makes it really easy to create desktop like web applications.

如果您确实需要所有这些功能来创建类似桌面的应用程序,我建议您尝试一下 ExtJS,因为它绝对是一个非常好的产品。大约 3 年前,我在某个项目中使用过它(第 2 版),我对他们用它所做的工作感到惊讶。它是功能非常丰富的产品,可以非常轻松地创建类似 Web 应用程序的桌面。

But if you're after a usual web site then a desktop-like experience is probably not what you should give to your visitors.

但是,如果您追求的是通常的网站,那么您可能不应该为访问者提供类似桌面的体验。