我正在通过ajax调用填充texbox值。我想更改当值被填充时触发的事件。
ajax 调用:
$.ajax({
url: 'ajaxExecute.aspx/GetCustInfoTeller',
data: strRequest,
dataType: "json",
contentType: "application/json",
cache: false,
context: document.body,
type: 'POST',
success: function (response) {
var saResponse = response.d.split("|");
$('#txtDateofBirth').val(saResponse[0]); // For this textbox change event gets fired
}
});
更改事件处理程序的代码:
$("#txtDateofBirth").bind('change', function () {
alert('1');
var today = new Date();
var birthDate = new Date($("#txtDateofBirth").val());
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
alert(age);
if (age > 7) {
$('#trMinor').hide();
}
else {
$('#trMinor').show();
}
});
通过 ajax 在 txtDateofBirth
中输入值时,我没有收到警报。
最佳答案
您必须手动触发该事件,只有当更改是由用户界面发起时才会自动触发。以编程方式设置值不会触发 change
事件。
您可以使用 .trigger()
触发事件jQuery方法:
$('#txtDateofBirth').val(saResponse[0]).trigger('change');
//or
$('#txtDateofBirth').val(saResponse[0]).change();
关于jquery - 文本框值更改事件没有被触发 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20992750/