简体   繁体   English

如何在代码中触发 jQuery 更改事件

[英]How to trigger jQuery change event in code

I have a change event that is working fine but I need to get it to recurse.我有一个工作正常的更改事件,但我需要让它递归。

So I have a function that is triggered on change that will "change" other drop downs based on a class selector (notice "drop downS", there could be more than one).因此,我有一个在更改时触发的函数,该函数将根据类选择器“更改”其他下拉列表(注意“下拉列表”,可能不止一个)。 This proxy change does not trigger the function and so fails.此代理更改不会触发该功能,因此会失败。 How can I get it to work?我怎样才能让它工作?

Code代码

$(document).ready(function () {
    var activeDropBox = null;

    $("select.drop-box").change(function () {
        var questionId = $(this).attr("questionId");
        var selectedAnswer = $(this).val();
        activeDropBox = this;

        alert(this.questionId);

        $.ajax(
        {
            type: "POST",
            url: answerChangedActionUrl,
            data: { questionId: questionId, selectedValue: selectedAnswer },
            success: function (data) {
                SetElementVisibility(data.ShowElement, questionId);
            }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('XMLHttpRequest:' + XMLHttpRequest.responseText);
                alert('textStatus:' + textStatus);
                alert('errorThrown:' + errorThrown);
            }
        });
    });

    function SetElementVisibility(visible, questionId) {
        // I would like each child to then trigger the change event...
        $(".childOf" + questionId)[visible ? 'show' : 'hide']('slow');
        
        // Suggested code
        //$(".childOf" + questionId + " select").trigger("change");

        if (!visible) {
            $(".childOf" + questionId + " select").attr('selectedIndex', 0);
        }
    }
}

The suggestions so far seem to work, but as the change event triggers an ajax post it now seems to fail here.到目前为止,这些建议似乎有效,但由于更改事件触发了 ajax 帖子,它现在似乎在这里失败了。 I'm going to play around with it but that is something for another question I feel.我会玩弄它,但这是我感觉的另一个问题。

使用trigger() 方法

$(selector).trigger("change");

For me为我

$('#element').val('...').change()

is the best way.是最好的方法。

The parameterless form of the change() method triggers a change event. change()方法的无参数形式会触发change事件。 You can write something like:你可以这样写:

$(document).ready(function() {
    $("#yourInitialElementID").change(function() {
        // Do something here...
        $(".yourDropDownClass").change();
    });
});
$(selector).change()

.change() 。改变()


.trigger("change") .触发器(“改变”)

Longer slower alternative, better for abstraction.更长更慢的替代方案,更适合抽象。

.trigger("change") .触发器(“改变”)

$(selector).trigger("change")

Use That :使用那个:

$(selector).trigger("change");

OR要么

$('#id').trigger("click");

OR要么

$('.class').trigger(event);

Trigger can be any event that javascript support.. Hope it's easy to understandable to all of You.触发器可以是 javascript 支持的任何事件.. 希望你们所有人都容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM