JavaScript不提交表单
JavaScript Not Submitting Form
我花了一个小时左右的时间在这里和谷歌上搜索,但没有成功。我有一个表单,我想通过POST提交。javascript运行和验证良好,它甚至"提交"表单。但是当它这样做时,它只是重新加载页面。没有发送POST头(使用Firefox中的Firebug确认)。这就好像它只是重新加载页面而不是提交它。
这个表单嵌入在addUser.php页面中。
<form action="addUser.php" method="post" name="NewUser">
<div data-role="field-contain">
<label label-for="EmpID" class="ui-hidden-accessible">Enter Employee ID</label>
<input name="EmpID" id="EmpID" placeholder="Employee ID" value="" autocomplete="off" title="Enter Employee ID" type="number" required autofocus>
<label label-for="UserName" class="ui-hidden-accessible">Enter username</label>
<input name="UserName" id="UserName" placeholder="Username" value="" autocomplete="off" title="Enter the new username" required>
<p>
<label label-for="NewPass1" class="ui-hidden-accessible">Enter Your Current Password</label>
<input name="NewPass1" id="NewPass1" placeholder="New Password" value="" type="password" autocomplete="off" title="Must be at least 8 characters, with numbers and upper and lower case letters." required>
</p>
<p>
<label label-for="NewPass2" class="ui-hidden-accessible">Enter Your Current Password</label>
<input name="NewPass2" id="NewPass2" placeholder="Verify Password" value="" type="password" autocomplete="off" title="Re-enter your chosen new password." required>
</p>
</div>
<button type="button" name="submitButton" value="submitButton" data-theme="d" data-inline="true" data-icon="check" data-iconpos="left" onclick="validateForm ()">
Add User
</button>
这里是Javascript:
function validateForm()
{
var reason = "";
reason += validateEmpID();
reason += validateUserName();
reason += validateNewPassword();
if (reason != "") {
alert("There are some errors with the data you entered:'n" + reason);
return false;
} else {
document.NewUser.submit();
}
}
我尝试了各种不同的方法来调用submit,结果都是一样的。非常感谢任何帮助。
谢谢加雷思
我觉得应该是
document.forms['NewUser'].submit();
或
document.forms.NewUser.submit();
我认为你在某个地方错过了添加返回false,这是浏览器要求重新加载表单的原因。更好的提交方式是使用提交按钮而不是简单的按钮。
为什么不使用AJAX post?或者更好的是一个jQuery AJAX POST?
$.post(url, data, datatype);
http://api.jquery.com/jQuery.post/function validateForm(event) {
var reason = "";
reason += validateEmpID();
reason += validateUserName();
reason += validateNewPassword();
if (reason != "") {
alert("There are some errors with the data you entered:'n" + reason);
event.preventDefault();
}
}
document.getElementById('my_form').addEventListener('submit', validateForm, false);
相关文章:
- HTML表单提交时未执行外部函数
- JS表单提交"无法使用Chrome数据保护程序加载此页面.尝试重新加载页面.调试信息:POST CISmtuK
- 将图像上传ajax与表单提交ajax相结合
- Javascript无法处理表单提交
- 如何在我的情况下禁用表单提交
- 加载后的页面与ajax表单提交不起作用
- angularjs-控制器在表单提交时未调用
- 禁用带有字符白名单的表单提交
- 如何使用Google Analytics跟踪表单提交
- 使用javascript将表单提交到iframe目标中
- e.preventDefault在表单提交时被忽略
- AngularJS JQuery Ajax表单提交等效
- JavaScript表单提交帮助
- PHP表单提交和重定向
- 当所有输入文本字段都为空时,禁止表单提交,但当jquery中的任何字段不为空时允许提交
- setTimeOut AFTER jQuery表单提交
- 从函数中选择要触发表单提交的正确元素
- JavaScript表单提交没有't fire asp服务器端点击功能的提交按钮
- 表单提交问题,如何在我的URL末尾获得ID的值
- Javascript使用Confirm取消表单提交