我有一个id为id的表单,theForm其中包含以下div,其中包含一个提交按钮:
<div id="placeOrder"
style="text-align: right; width: 100%; background-color: white;">
<button type="submit"
class='input_submit'
style="margin-right: 15px;"
onClick="placeOrder()">Place Order
</button>
</div>
Run Code Online (Sandbox Code Playgroud)
单击时,将placeOrder()调用该函数.该函数将上面div的innerHTML更改为"processing ..."(因此提交按钮现在消失了).
上面的代码有效,但现在的问题是我无法提交表单!我试过把它放在placeOrder()函数中:
document.theForm.submit();
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
我怎样才能提交表格?
我写了一个简单的脚本来验证登录表单,如下所示:
<script type="text/javascript">
function validateLoginForm(){
var idV = document.forms["LoginForm"]["id"].value;
var pwdV = document.forms["LoginForm"]["pwd"].value;
if (idV == null || idV == "" || idV.length < 5) {
alert("user ID must be filled and of more than 4 characters!!");
id.focus();
return false;
}
if (pwdV == null || pwdV == "" || pwdV.length < 5) {
alert("Password must be filled and of more than 4 characters!!");
pwd.focus();
return false;
}
return true;
}
</script>
Run Code Online (Sandbox Code Playgroud)
并称之为:
<form name="LoginForm" method="POST" onsubmit="return validateLoginForm()" action="Login" > …Run Code Online (Sandbox Code Playgroud) 当我提交表格时,我收到了一条警告信息.当我接受警报时,无论如何都会提交表格.返回false将被忽略.不能使用Onclick.我尝试var x = document.forms["form"]["fname"].value;
并且仍然相同.
<form id="f" method="post" name="form" onsubmit="return validateForm();" action="#">
<input type="text" name="fname" id="test" />
<input type="submit" value="submit"/>
</form>
<script type="text/javascript">
function validateForm() {
var x = document.getElementById('test').value;
if (x == null || x == 0 || x == "0") {
alert("Stop");
return false;
}
}
</script>
Run Code Online (Sandbox Code Playgroud)