javascript - 一键提交两份表格
问题描述
我需要帮助....
我正在尝试使用 xmlhttlrequest 通过一个提交按钮发送多个表单。但每次它只发送最后一个表格。
这是我的代码
for(j=0;j<collection;j++)
{
xhr.open("POST", "showQuestionnaire.php",true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(document.getElementById("submitAnswers"+j).submit());
}
我正在尝试在同一页面上使用 .submit() 提交表单并使用 $_GET 获取数据。但每次它跳过索引 0。
解决方案
试试下面的代码。
<!DOCTYPE html>
<html>
<head>
<title>Multiform - JAVASCRIPT</title>
</head>
<body>
<fieldset>
<legend>Form 1</legend>
<form name="f1" id="f1" onsubmit="return validate(this)">
<input type="text" name="username" placeholder="Username" />
</form>
</fieldset>
<fieldset>
<legend>Form 2</legend>
<form name="f2" id="f2" onsubmit="return validate(this)">
<input type="text" name="email" placeholder="Email" />
</form>
</fieldset>
<fieldset>
<legend>Form 3</legend>
<form name="f3" id="f3" onsubmit="return validate(this)">
<input type="text" name="password" placeholder="Password" />
</form>
</fieldset>
<button onclick="submitAll();">SUBMIT ALL</button>
<script>
'use strict';
function validate(form){
//forms processsing goes here...
console.log(form, form.name)
return false;
}
function submitAll(){
for(var i=0, n=document.forms.length; i<n; i++){
alert(i);
document.forms[i].onsubmit();
}
}
</script>
</body>
</html>
推荐阅读
- networkx - 在 Networkx 中组合图:将图添加为子节点
- sqlalchemy - Alembic 自动生成空迁移文件
- graph - 使用树的 Gremlin 查询(JSON 输出)
- python - 在另一个脚本结束后运行 python 脚本
- jenkins - Jenkins 使用 json 报告发布者
- python-3.x - 从公式字符串中删除指数
- python - 在python中使用bs4从div中的后代标签中刮取
- python - 从 jupyter 笔记本中的进程更新小部件
- c++ - 为什么 Boost 的 QuickSort 比 Julia 的 QuickSort 慢?
- android - 无法在 child() Android Studio Firebase 中为参数“pathString”传递 null