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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4247264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:34:23  来源:igfitidea点击:

How to trigger jQuery change event in code

javascriptjqueryjavascript-events

提问by 4imble

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. I'm going to play around with it but that is something for another question I feel.

到目前为止,这些建议似乎有效,但由于更改事件触发了 ajax 帖子,它现在似乎在这里失败了。我会玩弄它,但这是我感觉的另一个问题。

回答by John Hartsock

Use the trigger() method

使用trigger() 方法

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

回答by Luis Carlos Steffens

for me $('#element').val('...').change()is the best way.

对我来说$('#element').val('...').change()是最好的方法。

回答by Frédéric Hamidi

The parameterless form of the change()method triggers a changeevent. You can write something like:

change()方法的无参数形式会触发一个change事件。你可以这样写:

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

回答by Geoffrey Hale

$(selector).change()

.change()

。改变()



.trigger("change")

.触发器(“改变”)

Longer sloweralternative, better for abstraction.

更长更慢的替代方案,更适合抽象。

.trigger("change")

.触发器(“改变”)

$(selector).trigger("change")

回答by Muhammad Fahad

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 支持的任何事件.. 希望你们所有人都容易理解。