如何在以编程方式检查属性时触发 jQuery 单击处理程序?

How to get a jQuery click handler to fire when an attribute is checked programmatically?

我有两个开始隐藏的 DropDownLists:

$(window).load(function () {
    . . . other code elided for brevity

    $('[id$=ddlPayToIndividual]').hide();
    $('[id$=ddlPayToVendor]').hide();
});

...但根据单击相应的单选按钮显示一个:

$(document).on("click", '[id$=rbPaymentToIndividual]', function () {
    if (this.checked) {
        $('[id$=ddlPayToVendor]').slideUp();
        $('[id$=ddlPayToIndividual]').slideDown();
    }
});

$(document).on("click", '[id$=rbPaymentToVendor]', function () {
    if (this.checked) {
        $('[id$=ddlPayToIndividual]').slideUp();
        $('[id$=ddlPayToVendor]').slideDown();
    }
});

当用户单击单选按钮(rbPaymentToIndividual 或 rbPaymentToVendor)时,这一切都有效;但是,当我以编程方式检查它时:

$(document).on("click", '[id$=rbPaymentForSelf]', function () {
    if (this.checked) {
        . . . other code elided for brevity
        $('[id$=rbPaymentToIndividual]').attr('checked', true);      
    }
});

...不是。为什么?是否有解决方法让它将编程属性操作视为等同于用户的直接参与?

您可以在输入上手动触发 change 事件:

$('[id$=rbPaymentToIndividual]').trigger('change');

正如@tin 指出的那样 - 这是除了设置 属性.

您试过触发点击事件吗?

$('[id$=rbPaymentToIndividual]').prop("checked", true).click();