用onBlur jquery替换onKeyUp

时间:2016-01-08 09:38:06

标签: javascript jquery

<input class="text  empty" type="text" size="20" name="Email" id="Email" value="" onkeyup="checkconditions(this.value, this.name, this.type);">

如何使用jquery在on blur上更改onKeyup,以便用户在离开电子邮件字段时会收到验证消息?

2 个答案:

答案 0 :(得分:2)

使用jQuery blur()并将onkeyup设置为null,如下面的代码段所示。

$(document).ready(function() {
  document.getElementById('Email').onkeyup = null;
  $('#Email').blur(function() {
    checkconditions(this.value, this.name, this.type);
  });
});

function checkconditions(v, n ,t) {
  alert('Blur');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="text  empty" type="text" size="20" name="Email" id="Email" value="" onkeyup="checkconditions(this.value, this.name, this.type);">

答案 1 :(得分:0)

替换

<input class="text  empty" type="text" size="20" name="Email" id="Email" value="" onkeyup="checkconditions(this.value, this.name, this.type);">

<input class="text  empty" type="text" size="20" name="Email" id="Email" value="" onBlur="checkconditions(this.value, this.name, this.type);">