jquery綁定input的change事件
背景:在做一個登錄頁時,如果用戶未輸入驗證碼則無法點擊登錄按鈕,所以想到了用input的change事件,但是在寫完后發現無法監聽input值的改變。
解決辦法:改為了input事件
input的change事件(相當於blur事件)
用戶在輸入完成后失去焦點才會觸發,不能實時監聽輸入框值的變化,相當於blur事件
//這種情況就是在輸入完成后失去焦點才能觸發
$('input[name="h5logincode"]').on('change', function(){
var _this = $(this);
if(_this.val().length > 0){
$('.sub').css('background-color', '#FFBC45');
$('.sub').attr('disabled', false);
}else{
$('.sub').attr('disabled', true);
$('.sub').css('background-color', '#b0aeae');
}
});
input的input事件
用戶輸入的內容改變時觸發,相當於實時監聽
//驗證碼輸入后登錄按鈕啟用
$('input[name="h5logincode"]').on('input', function(){
var _this = $(this);
if(_this.val().length > 0){
$('.sub').css('background-color', '#FFBC45');
$('.sub').attr('disabled', false);
}else{
$('.sub').attr('disabled', true);
$('.sub').css('background-color', '#b0aeae');
}
});