jQuery .trigger("change") 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18693463/
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-26 22:13:35  来源:igfitidea点击:

jQuery .trigger("change") not working

javascriptjquery

提问by Robin

I can't get .trigger("change")to work. Anyone know why?

我不能.trigger("change")上班。有谁知道为什么?

jQuery(document).ready(function () {

    jQuery("select[id='DROPDOWNID']").change(function () {

        var selectedIndex = jQuery("select[id='DROPDOWNID']").prop('selectedIndex');

        switch (selectedIndex) {
            case 0:
                hideVarforProspekt();
                break;
            case 1:
                hideOrdervarde();
                break;
            case 2:

                break;
            case 3:
                hideSektor();
                break;
        }
    });

    ** jQuery("select[id='DROPDOWNID']").trigger("change"); **


    function hideVarforProspekt() {
        jQuery("input[id='IDFROMSHAREPOINT']").closest('tr').hide();
    }

    function hideSektor() {
        jQuery("table[id='IDFROMSHAREPOINT']").closest('tr').hide();
    }

    function hideUppskOrder() {
        jQuery("input[id='IDFROMSHAREPOINT']").closest('tr').hide();
    }
});

采纳答案by Krasimir

Sometimes the usage of trigger is not necessary:

有时不需要使用触发器:

// use just jQuery("#DROPDOWNID") instead
var select = jQuery("select[id='DROPDOWNID']");

// placing the handler in separate function
var changeHandler = function () {
    var selectedIndex = select.prop('selectedIndex');
    switch(selectedIndex) {
        case 0:
            hideVarforProspekt();
        break;
        case 1:
            hideOrdervarde();
        break;
        case 2:

        break;
        case 3:
            hideSektor();
        break;
    }
}

// cache your jQuery selectors. It's a good practice
// and improves the readability
select.change(changeHandler);

// triggering
changeHandler();

回答by Nilesh

Change event handler should be defined before calling the .trigger('change')or .change()

应在调用.trigger('change')或之前定义更改事件处理程序.change()

Below are 2 scenarios.

下面是2个场景。

Scenario 1:where change event is called before it is defined.

场景 1:在定义之前调用更改事件。

    $(document).ready(function() 
    {
       $('#selector').change();   or  $('#selector').trigger('change');
       $('#selector').on('change', function(){

        });
    });

Scenario 2:where change event handler is defined before calling it

场景 2:在调用它之前定义更改事件处理程序

$(document).ready(function() 
{
    $('#selector').on('change', function(){

    });
    $('#selector').change();   or  $('#selector').trigger('change');
});

For me issue was fixed when I use scenario2. Hope this helps!!

对我来说,使用场景 2 时问题已解决。希望这可以帮助!!

回答by Arun

Here is a working version using the id syntax:

这是使用 id 语法的工作版本:

$(document).ready(function () {
  $('select[id=car]').change(function () {
      alert("change: " + $(this).val());
  });

  $("#car").trigger('change');
});

JSFiddle

JSFiddle

回答by tsveti_iko

The .change()event waits until the input field loses focus, so it won't be fired before you click or tab out of the field.

.change()事件会一直等到输入字段失去焦点,因此在您单击或跳出该字段之前不会触发它。

So you can just add .blur()event after the .change()event like this:

所以你可以像这样在.blur()事件之后添加事件.change()

     var $element = jQuery("select[id='DROPDOWNID']");
     $element.change(function () { ... });
     $element.blur();

Or you can use another event instead of .change(), e.g. .keyup(), .paste(), etc.

或者你可以使用另一个事件来代替.change(),如.keyup().paste()

回答by Shakti Patel

used this :

用过这个:

jQuery("#DROPDOWNID").trigger("change");

can you please check given link Demo

你能检查给定的链接演示

HTML Code

HTML代码

<select id="car">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

Jquery Code

查询代码

$(document).ready(function(){
  $("#car").change(function(){
    alert("change");
  });

    $("#car").trigger('change');
});

Result :

结果 :

It fire alert

它发出警报

Remove id=from all selector and replace it with #selected id

从所有选择器中删除id=并将其替换为#selected id

回答by Pankaj Wanjari

try this , it should work

试试这个,它应该工作

$('#selector').change();