[英]How to trigger an event when the user clears the text field and focus out of the text field using Jquery?
當用戶清除文本字段並從同一焦點移出焦點時,我需要編寫一個事件處理程序。
我正在使用以下功能來捕獲“聚焦”事件。
$("input[type=text]").blur(function () {
}
我具有以下功能來捕獲明場事件。
$("input[type=text]").keyup(function() {
if (!this.value) {
}
}
我嘗試在blur()中使用keyup()函數,因為我需要捕獲焦點,然后清除字段。 這是我的代碼的樣子:
$("input[type=text]").blur(function () {
$(this).keyup(function() {
if (!this.value) {
}
}
}
但這是行不通的。 甚至在焦點移出場之前也會觸發清除場事件。 而且,它多次觸發事件。 這里有什么問題?
我認為這更簡單:
$('input').on('blur', function(e) {
if(!$(this).val()) {
// IS NO VALUE IN THE INPUT
$(this).trigger('blur'); // trigger the blur event
}
});
這個給你:
$("input[type=text]").on('blur', function() { alert('blur'); }); $("input[type=text]").on('input', function() { if (!$(this).val()) { alert("input nothing"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" />
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.