当任何输入为空时,我无法阻止表单提交。它没有错误,但它也没有停止提交。我在表单提交输入中调用了该函数。它位于onClick调用之下。
JS档案
$state
HTML文件
TX
答案 0 :(得分:2)
使用2017-08-26 20:18:52.006 ERROR 2784 --- [nio-8080-exec-4] org.hibernate.AssertionFailure : HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException
2017-08-26 20:18:52.009 WARN 2784 --- [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
属性,您甚至不需要任何JavaScript。请参阅演示2.有关正常运行的演示,请参阅此PLUNKER
在每个required
添加return false
e.preventDefault()

function stopSubmit(e) {
var inDay = document.getElementById(indate).value;
var inType = document.getElementById(intype).value;
var inAmount = document.getElementById(inamount).value;
if (inDay == "") {
alert("Please select a date");
e.preventDefault();
return false;
}
if (inType == "Select One") {
alert("Please select a frequency");
e.preventDefault();
return false;
}
if (inAmount == "") {
alert("Please enter an amount");
e.preventDefault();
return false;
} else {
alert("Your form was submitted");
}
}

<form>
<td>
<input type="submit" name="submitincome" value="submit" onclick="stopSubmit()">
</td>
</form>
&#13;
答案 1 :(得分:0)
我能够看到你在哪里犯了错误,document.getElementById()
接受一个字符串作为参数,但你碰巧传递了一个未定义的变量
function stopSubmit(){
var inDay = document.getElementById('indate').value;
var inType = document.getElementById('intype').value;
var inAmount = document.getElementById('inamount').value;
if (inDay === "") {
alert("Please select a date");
return false;
}
if (inType == "Select One"){
alert("Please select a frequency");
return false;
}
if (inAmount === ""){
alert("Please enter an amount");
return false;
}
else {
alert("Your form was submitted");
}
}