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

时间:2021-01-30 18:57:56

I have two DropDownLists that start off hidden:

我有两个开始隐藏的DropDownLists:

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

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

...but one is shown based on which corresponding radio button is clicked:

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

$(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();
    }
});

This all works when the user clicks either radio button (rbPaymentToIndividual or rbPaymentToVendor); however, when I programmatically check it:

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

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

...it doesn't. Why? Is there a workaround to get it to consider a programmatic attribute manipulation tantamount to a user's direct involvement?

......它没有。为什么?是否有一种解决方法可以让它考虑程序属性操作等同于用户的直接参与?

2 个解决方案

#1


1  

Have you tried firing the click event?

你试过触发点击事件了吗?

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

#2


1  

You can manually trigger a change event on the input:

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

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

as @tin points out - this is in addition to setting the property.

正如@tin所指出的 - 这是设置属性的补充。

#1


1  

Have you tried firing the click event?

你试过触发点击事件了吗?

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

#2


1  

You can manually trigger a change event on the input:

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

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

as @tin points out - this is in addition to setting the property.

正如@tin所指出的 - 这是设置属性的补充。