我試圖得到一個警報,如果我離開一個空的領域,但它不工作,它只是沒有調用該函數去下一步。我忘了把「的onsubmit =」返回確認()」我的代碼有什麼問題,是不是調用函數,JSP
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type =text/javascript>
function validation() {
var errormessage = "";
if (document.getElementById('firstName').value == "") {
errormessage += "please enter the firstname \n";
document.getElementById("firstName").style.borderColor = "red";
}
if (errormessage != "") {
alert(errormessage);
return false;
}
}
</script>
</head>
<body>
<form action ="anotherpage.jsp" method="post" autocomplete="on" onsubmit="return validation()>
Firstname:<input type="text" id="firstName" name="name" value= " <%=name%>" />
<br>
<input type="submit" value="Next Step" name="nextStep" />
</form>
</body>
</html>
那麼,你永遠不會在任何地方調用'validation()'。 –
另外,如果你不想讓它自動移動,你需要防止'submit'按鈕的默認行爲 –
對不起,我忘記把「onsubmit =」return validation()「...調用函數, –