如何在用户通过jQuery在键盘上键入时删除警报消息?

时间:2012-10-25 22:02:17

标签: javascript jquery syntax

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Save new student</title>
<script>

function insertdata()
{

var n = document.getElementById("name").value;
var f = document.getElementById("family").value;
var m = document.getElementById("mark").value;
var t = document.getElementById("tell").value;

 if ((n=="") || (f=="") || (m=="") || (t=="")){


  var error=document.getElementById("error").innerHTML="fill out the form with valid values";

 }
 **else if()**
 {


 }
 else{

frminsert.submit();


 }

 }


</script>
</head>

<body>

please enter your data:

<br/>

 <form action="saveinsert.php" method="post" name="frminsert">
  Name :
 <input type="text" name="name" id="name" />
 <br/>
 Family :
 <input type="text" name="family" id="family" />
 <br/>
 Mark :
<input type="text" name="mark" id="mark" />
<br/>
 Tell :
<input type="text" name="tell" id="tell" />
<br />
 <input type="button" value="Save New Student"  onclick="insertdata();" />

 <input type="reset" value="Reset Form" />

</form>
<p id="error"></p>
<a href="show.php">show the list of students</a>
</body>
</html>


我的代码将使用此语句指示错误消息:正确填写表单。
我将详细说明,当用户开始在键盘上输入时,我需要消除上面提到的错误信息。

2 个答案:

答案 0 :(得分:3)

我认为这是你需要的吗? (清除错误消息)

document.getElementById("error").innerHTML= "";

如果您希望在用户执行某些操作(例如类型为框)时发生这种情况,那么您需要在该输入上添加一个事件监听器,例如

<input onKeyDown="removeErrorMessage()" ... />
...
<script>
function removeErrorMessage{
document.getElementById("error").innerHTML= "";
};
</script>

答案 1 :(得分:0)

你可以把它放到第一行:

function cleanError()
{
document.getElementById("error").innerHTML= "";
}

并且每个输入都会onBlur="cleanError()“像这样:

<input onBlur="cleanError()" type="text" name="family" id="family" />

祝你好运!