Javascript逻辑不起作用

时间:2017-07-08 07:59:40

标签: javascript jquery logic

ifelse if工作时else条件不起作用:



function userchk() {
  var un = $('#user_name').val();
  if (un.length < 3 && un.length != 0) {
    $("#validateUser").html("<span style='color:red' class='status-not-available'>User Name Shoul Be Greater Then 3.</span>");
  }
  else if (un.length == 0) {
    $('#validationUser').html("<span style='color:red' class='status-not-available'> User Name Cannot Be Empty.</span>");
  }
  else {
    $('#validationUser').html("");
  }
}

$('#btnTest').on('click', function() {
  userchk();
});
&#13;
input {
  width: 100%;
  margin-top: 20px;
  float: left;
}

button {
  font-size: 20px;
  background-color: #555;
  color: #fff;
  padding: 10px 30px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button id="btnTest">Test userchk()</button>

<input type="text" name="user_name" id="user_name" onBlur="userchk()" class="form-control" placeholder="Username" />
<div id="validationUser"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的if条件会导致选择并操纵ID为 #validateUser 的元素。

您的else ifelse目标 #validationUser

Javascript逻辑条件合理但您没有获得预期的结果,因为您的目标是if块中的其他元素。

相关问题