在JavaScript中单击提交后,如何获得动态形式值?
我有一个动态形式数据,该数据是从后端加载的。如果有静态形式,我可以通过document.getElementById(“ xxx”)获得值。但是,如果有动态形式未知ID,我该如何获得值?我想以这些格式提交
"answers" :[
{
"questionId" : 1002,
"response" : "Yes"
},{
"questionId" : 1001,
"response" : "No"
}
]
I have a dynamic form data which is loaded from backend. If there are static form, I can get value by document.getElementById("xxx"). However, if there are dynamic form which is unknown id, how can I get the value ? I would like to have these format to submit on it
"answers" :[
{
"questionId" : 1002,
"response" : "Yes"
},{
"questionId" : 1001,
"response" : "No"
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的表单字段放在
表单
标签中(包括提交按钮)。
给表格的类,例如
自定义形式
在
.custom-form
selector上添加提交侦听器。在提交处理程序中,通过做某事获取输入字段
像这样:
您现在可以循环通过它们,并且
执行您所需的操作。
Put your form fields inside a
form
tag (including the submitbutton).
Give a class to the form e.g.
custom-form
Add a submit listener on the
.custom-form
selector.Inside the submit handler, get the input fields by doing something
like this:
You can now loop through them and
perform your desired operation.
当您在表单上循环时,您可以从后端获取的数据中生成类名称或ID,也可以构造新的名称。
When you are looping over the form, you can generate class name or id from data fetched from backend, or you can construct new one.