我添加的eventhandler仅在加载页面时才工作一次

时间:2017-02-18 18:24:41

标签: javascript eventhandler

我正在尝试检查用户是否仍然为空时更改字段。我在用户名字段中添加事件监听器,因此当用户从一个字段移动到另一个字段时,他会得到一个检查符号或者错误。
我的HTML文件:

<!DOCTYPE html>
<html>
    <head>
        <title>Registeration form</title>
        <link type="text/css" rel="stylesheet" href="css/fontawesome.css"/> 
    </head>
    <body>
        <form method="post">
            <fieldset>
                <input type="text" name="user_name" id="user_name" style="display:inline;float:left;" placeholder="User Name">
                <li id="name_status" style="list-style-type:none;"></li>
            </fieldset>
            <fieldset>
                <input type="text" name="email" id="email"  style="display:inline;float:left;" placeholder="Email Address">
                <li id="email_status" style="list-style-type:none;"></li>
            </fieldset>
            <fieldset>
                <input type="button" name="submit" id="submit" value="submit">
            </fieldset>
        </form>
    <script type="text/javascript" src="js/test.js"></script>
    </body>
</html>

我的Javascript文件:

var user_name=document.getElementById("user_name");
var user_name_status=document.getElementById("name_status");

user_name.addEventListener("blur",check_empty(user_name.value,user_name_status),false);
function check_empty(value,status){
    if(value.length===0){
        status.innerHTML="<sup>*</sup>This feild cannot be empty";
    } else {
        status.innerHTML="<i class='icon-check'></i>";
    }
}

2 个答案:

答案 0 :(得分:0)

您正在立即致电check_empty。您可以将函数引用check_empty传递给.addEventListener()。在事件处理程序中,this引用user_name。而不是传递变量status,而是在事件处理程序中使用定义的varibale user_name_status

var user_name = document.getElementById("user_name");
var user_name_status = document.getElementById("name_status");

user_name.addEventListener("blur", check_empty, false);

function check_empty(e) {
  if (this.value.length === 0) {
    user_name_status.innerHTML = "<sup>*</sup>This feild cannot be empty";
  } else {
    user_name_status.innerHTML = "<i class='icon-check'>check</i>";
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>Registeration form</title>
</head>

<body>
  <form method="post">
    <fieldset>
      <input type="text" name="user_name" id="user_name" style="display:inline;float:left;" placeholder="User Name">
      <li id="name_status" style="list-style-type:none;"></li>
    </fieldset>
    <fieldset>
      <input type="text" name="email" id="email" style="display:inline;float:left;" placeholder="Email Address">
      <li id="email_status" style="list-style-type:none;"></li>
    </fieldset>
    <fieldset>
      <input type="button" name="submit" id="submit" value="submit">
    </fieldset>
  </form>
</body>

</html>

答案 1 :(得分:0)

我认为最好的选择是在匿名函数中传递带有参数的函数,如下面的代码所示。

&#13;
&#13;
var user_name = document.getElementById("user_name");
var user_name_status = document.getElementById("name_status");  

user_name.addEventListener("blur",function(){check_empty(user_name.value,user_name_status);},false);
function check_empty(name,status) {
  if (name.length === 0) {
    status.innerHTML = "<sup>*</sup>This feild cannot be empty";
  } else {
    status.innerHTML = "<i class='icon-check'>check</i>";
  }
}
&#13;
<!DOCTYPE html>
<html>

<head>
  <title>Registeration form</title>
</head>

<body>
  <form method="post">
    <fieldset>
      <input type="text" name="user_name" id="user_name" style="display:inline;float:left;" placeholder="User Name">
      <li id="name_status" style="list-style-type:none;"></li>
    </fieldset>
    <fieldset>
      <input type="text" name="email" id="email" style="display:inline;float:left;" placeholder="Email Address">
      <li id="email_status" style="list-style-type:none;"></li>
    </fieldset>
    <fieldset>
      <input type="button" name="submit" id="submit" value="submit">
    </fieldset>
  </form>
</body>

</html>
&#13;
&#13;
&#13;