选择性提交表单元素

发布于 2024-12-08 22:41:13 字数 514 浏览 1 评论 0原文

我有一个包含隐藏元素的表单,用于创建一个逗号分隔值的伪数组,该数组将通过 post 提交到服务器,然后隐藏元素将被解码为数组并进行处理以进行存储。为了填充隐藏元素,我使用可见元素和 javascript 处理的按钮向隐藏元素添加值,每次按下按钮时都会清除表单。

问题是:如何让可见元素不提交到服务器,而只提交隐藏元素以节省带宽?有没有办法创建一个文本输入字段,该字段不与表单的其余部分一起提交,但 javascript 和普通表单控件仍然可以访问?这里的目标是防止在 JavaScript 用我需要的数据填充伪数组后,单击提交按钮时不必要地重复发送相同的数据。

编辑:感谢您的帮助。我得到的前两个答案很好,但我选择了一个我认为更详细并且对我自己和任何其他可能正在寻找相同解决方案的人有帮助的答案。

计划:我将有一个 onsubmit 脚本,它在提交之前禁用不需要的字段,以便它们不会发送到服务器,从而节省(一点点)带宽并减少我的服务器端脚本需要的信息量做。这使得可以轻松地使用 javascript 清除我想要清除的字段,同时不断地保持隐藏字段加载我需要的 CSV。

I have a form that has hidden elements in it to create a pseudo-array of comma-separated values that will be submitted to the server through post, where the hidden elements will then be decoded into arrays and processed for storage. To fill the hidden elements, I use visible elements and a button that javascript handles to add values to the hidden elements, clearing the form every time the button is pressed.

Here is the question: How do I get the visible elements NOT to be submitted to the server and only submit the hidden elements in an effort to save bandwidth? Is there a way to create a text entry field that doesn't get submitted with the rest of the form, but that javascript and normal form controls can still access? The goal here is to prevent unnecessary repeats of the same data being sent when the submit button is clicked, AFTER javascript has filled the pseudo-arrays with the data I need.

EDIT: Thanks for the help. The first two answers I got were good, but I chose as an answer the one I thought was a little more detailed and helpful to myself and anyone else who may be looking for the same solutions.

PLAN: I'll have an onsubmit script that disables unneeded fields just before submit so that they don't get sent to the server, thereby saving (a tiny bit of) bandwidth and reducing the amount of information my server-side script needs to do. This keeps it possible to easily use javascript to clear the fields I want cleared while constantly keeping the hidden fields loaded with the CSV's I need.

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

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

发布评论

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

评论(2

杀手六號 2024-12-15 22:41:13

我现在能想到两个证明:

  1. 将可见的输入元素放在 form 标记之外,只保留提交按钮和隐藏字段在里面。
  2. 在表单元素上创建一个事件 onsubmit 以在可见字段上设置 disabled 属性。在某些可能需要另外删除该事件的浏览器上,返回 false 并手动触发表单提交。

There are two provabilities that I can think of now:

  1. Put visible input elements outside form tag, leave only submit button and hidden fields inside.
  2. Create an event onsubmit on form element to set disabled property on visible fields. On some browser that may require to additionally remove that event, return false and trigger form submission manually.
蓝眼睛不忧郁 2024-12-15 22:41:13

您可以将元素的“disabled”属性设置为true以防止它们被提交。

You can set the "disabled" property of the elements to true in order to prevent them from being submitted.

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