[英]jQuery - Check if specific word in text box has been deleted and change class of element
[英]jquery check if text has been deleted from input field
我正在使用引導程序提前輸入。 當用戶在表單中輸入地址時,將顯示一個下拉列表,其中包含可能的地址。 當他們選擇一個時,我添加了一個隱藏的輸入字段,其中包含數據庫的地址ID。
現在,當用戶從輸入字段中刪除文本時,該地址不再有效,因此我想刪除該隱藏的輸入字段。 問題是如何檢測用戶何時使用Delete鍵或鼠標從輸入字段中刪除文本。
我已經嘗試過keypress事件,在使用Delete鍵時它不起作用:
var input_changed = false
$('#myid').bind('typeahead:selected', function(obj, datum, name) {
$.ajax({
url: "/lead_profiles/populate_address",
data: {
address : datum
}
// dataType: "script"
}).done(function(data){
var contact = JSON.parse(data)[0];
var $parent = $("#myid").closest(".form-group");
$parent.after('<input name="lead_profile[contact_attributes][id]" type="hidden" value="' + contact.id + '">')
input_changed = true
});
});
$("#myid").on('keypress', function(){
if(input_changed){
alert("Input has changed");
}
})
除了按鍵,我還需要另一個事件來確定文本是否已從輸入字段中刪除。
您可以使用oninput事件:{'keyup paste'用於不支持它的舊版瀏覽器}
$("#myid").on('input', function(){
if(input_changed){
alert("Input has changed");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.