如何单击一个按钮同时触发多个表单/提交按钮
我有一些选项卡,每个选项卡都包含自己的表单和自己的提交按钮。我想通过单击一个按钮来触发所有表单并发送每个表单的请求。该按钮本质上应该单击每个表单的每个单独的提交按钮(在这种情况下,该按钮将是类“submit_all”的按钮。html:
div class="tab-content" id="tab_content_one"
div id="#tab1" class="tab-pane fade in active"
= form_tag({ action: "route1" }, method: method)
.form-group
textarea.form-control name="words"
input.btn.btn-primary type="submit" class="submit"
div id="tab2" class="tab-pane fade"
= form_tag({ action: "route1" }, method: method)
.form-group
textarea.form-control name="words"
input.btn.btn-primary type="submit" class="submit"
div id="tab3" class="tab-pane fade"
= form_tag({ action: "route1" }, method: method)
.form-group
textarea.form-control name="words"
input.btn.btn-primary type="submit" class="submit"
input.btn.btn-primary type="submit" class="submit_all" onclick="goBack()"
我尝试使用 javascript 检索每个表单的所有提交按钮并迭代然后单击每个提交按钮,但这不起作用,最终只发出了最终表格。
function goBack() {
var submit2 = document.getElementsByClassName('submit')
console.log(submit2)
for (i=0; i<=(submit2.length); i++){
submit2[i].click()
}
我做错了什么吗?
I have some tabs, each containing their own form and their own submit button. I want to fire off all the forms and send requests for each form at the click of one button. That one button should essentially click each individual submit button for each form (in this case that button would be the button with class "submit_all". The html:
div class="tab-content" id="tab_content_one"
div id="#tab1" class="tab-pane fade in active"
= form_tag({ action: "route1" }, method: method)
.form-group
textarea.form-control name="words"
input.btn.btn-primary type="submit" class="submit"
div id="tab2" class="tab-pane fade"
= form_tag({ action: "route1" }, method: method)
.form-group
textarea.form-control name="words"
input.btn.btn-primary type="submit" class="submit"
div id="tab3" class="tab-pane fade"
= form_tag({ action: "route1" }, method: method)
.form-group
textarea.form-control name="words"
input.btn.btn-primary type="submit" class="submit"
input.btn.btn-primary type="submit" class="submit_all" onclick="goBack()"
I tried using javascript to retrieve all the submit buttons for each of the forms and iterating through and clicking each submit button however that did not work. It ended up only firing off the final form.
function goBack() {
var submit2 = document.getElementsByClassName('submit')
console.log(submit2)
for (i=0; i<=(submit2.length); i++){
submit2[i].click()
}
Am I doing something wrong? and how else can I go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在所有表单上发送
form.submit()
事件(或提交按钮上的button.click()
),则只能提交一个表单,具体取决于表格是如何构建的。要从所有表单获取数据,您可以使用 new FormData 收集它们(FormElement) 并使用
[...formData.entries()]
序列化它们。If you send a
form.submit()
-event (orbutton.click()
on the submit button) on all forms, only one form may be submitted, depending on how the forms are build.To get the data from all forms you can gather them with new FormData(FormElement) and serialize them using
[...formData.entries()]
.