我只是在练习js。我试图在表单中做一个非常简单的验证,但是它正如我所期望的那样出现了错误。 这里是我的代码:Javascript“onsmit”事件不能正常工作
var form = document.getElementById('form');
var name = document.getElementById('name');
var email = document.getElementById('email');
var msg = document.getElementById('message');
var error = document.getElementById('error');
function handlingForm() {
form.onsubmit = function(c)
{
if (name.value == "")
{
error.innerHTML = "Error Submiting Form !";
return false;
}
else
{
error.innerHTML = "You have successfuly submited the Form..! Congrats ;)";
return true; // ;) Just Kidding :D
}
};
}
window.onload = function(c)
{
handlingForm();
}
<!DOCTYPE html>
<html>
<head>
<title>jsForm</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="container">
<form id="form">
<input type="text" placeholder="Name" id="name">
<input type="text" placeholder="Email" id="email">
<textarea rows="4" placeholder="Message" id="message"></textarea>
<input type="submit" value="Send" id="send">
<p id="error"></p>
</form>
</div>
<script src="script.js"></script>
</body>
</html>
的问题是,它不进行验证。每次它提交时返回true,但是当我将“name.value”替换为“email.value”时,代码将起作用。我现在不知道现在有什么问题。如果有人可以帮助我..
仍然没有工作.. –
它适用于我,请重试。 – MaxZoom
对不起。等待我试试 –