【问题标题】:textbox value change event not getting fired jquery文本框值更改事件没有被触发 jquery
【发布时间】:2014-01-08 10:20:12
【问题描述】:

我正在通过 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 中输入值时,我没有收到警报

【问题讨论】:

  • 您使用的是旧版本的 jQuery (.on() 而不是.bind()
  • 触发事件,$('#txtDateofBirth').val(saResponse[0]).change()

标签: ajax forms javascript-events jquery


【解决方案1】:

您必须手动触发事件,只有在用户界面发起更改时才会自动触发。以编程方式设置值不会触发change 事件。

您可以使用.trigger() jQuery 方法触发事件:

$('#txtDateofBirth').val(saResponse[0]).trigger('change');
//or
$('#txtDateofBirth').val(saResponse[0]).change();

【讨论】:

    猜你喜欢
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2020-01-31
    • 1970-01-01
    • 2010-11-01
    相关资源
    最近更新 更多