是否可以在表单提交的同时提交 JavaScript 数组?

发布于 2024-09-10 20:45:48 字数 304 浏览 5 评论 0原文

我想为网络应用程序创建一个类似列表的表单。元素被添加到列表、修改列表和从列表中删除,用户单击“提交”将列表发送到 Web 应用程序以进行进一步处理。

我想要有两个可以添加数据的 INPUT 字段,单击“添加”按钮,数据就会添加到两个 DOM 中(以便用户可以看到它,并通过以下方式删除它)单击“删除”按钮)并保存到将与较大表单一起提交的 JavaScript 数组。在构建列表之前,我不希望 Web 应用程序有任何流量。

这可行吗?我的 HTML 有点生疏,而且我还没有掌握最新的 JS 库。您是否会推荐使用代码示例来完成此任务的框架?

I would like to create a list-like form for a web application. Elements are added to, modified and removed from the list, and the user clicks "Submit" to send the list to the web application for further processing.

I would like to have two INPUT fields that I can add data into, click an "Add" button, and the data is added to both the DOM (so the user can see it, and remove it by clicking a "Remove" button) and to a JavaScript array that will be submitted along with the larger form. I do not want any traffic to the web application until the list is built.

Is this doable? My HTML is a bit rusty and I haven't kept on top of the latest JS libraries. Are there frameworks you would recommend for accomplishing this, with code samples?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-09-17 20:45:48

最简单的方法可能是将值数组放入 JSON 对象中,然后将该对象作为字符串值嵌入到表单上的隐藏元素中。

然后,在处理表单的服务器端代码中,您应该能够非常轻松地解码 JSON 对象。例如,在 PHP 中,这是一个调用 json_decode() 的简单过程。

Probably the easiest way to do this would be to put the array of values into a JSON object, and then embed that object as a string value into a hidden element on the form.

Then in the server-side code in which you process the form, you should be able to decode the JSON object pretty easily. For instance, in PHP, this is a simple process of calling json_decode().

依 靠 2024-09-17 20:45:48

当然你可以做到这一点。

我可以想出很多办法。一种是创建一个变量,将列表项存储在数组中。当您与 UI 中的列表项交互时,您还可以更新数组。

单击“提交”按钮后,将阵列粘贴到隐藏的“输入”中以发送回服务器。

也就是说……如果在 UI 中动态创建/隐藏内容,人们可能会期望通过 AJAX 实时更新服务器。因此,请确保您的 UI 设计清楚地表明,除非用户执行“保存”操作,否则它不会更新。

至于框架,事实上的答案是 jQuery。

Sure you can do this.

I can think of a number of ways. One is to create a variable that stores the list items in an array. As you interact with the list items in the UI, you can also updated the array.

Upon click of the SUBMIT button, stick the array into a hidden INPUT for sending back to the server.

That said...if things are being created/hidden on the fly in the UI, a person might expect that to be updating the server real-time via AJAX. So make sure your UI design clearly shows that it won't be updated UNTIL a user performs the SAVE action.

As for a framework, the defacto answer would be jQuery.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文