jquery绑定input的change事件

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');
	}
});
原文地址:https://www.cnblogs.com/alisleepy/p/11200329.html