javascript jQuery 触发器在 IE 中不起作用。为什么?

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

jQuery trigger not working in IE. Why?

javascriptjqueryinternet-explorer-7triggers

提问by

$('#XynBp0').find('input').each(function(){
    if ($(this).attr('value') == 'Cancel'){
        $(this).trigger('click');
    }
});

doesn't work in IE7

在 IE7 中不起作用

采纳答案by Mouhannad

it's strange but try to create a custom event

这很奇怪,但尝试创建一个自定义事件

$('#XynBp0 input').bind('custom',function(){
 //code
})


$('#XynBp0').find('input').each(function(){
    if ($(this).attr('value') == 'Cancel'){
        $(this).trigger('custom');
    }
});

Does this work?

这行得通吗?

回答by Jason S.

$.click()(or $.trigger('click')) doesn't simulate a mouse click; it fires off any onclickevents bound to that element. If you haven't assigned an onclickevent to that input you're searching for, nothing will happen.

$.click()(或$.trigger('click')) 不模拟鼠标点击;它会触发绑定到该元素的任何onclick事件。如果您没有为您正在搜索的输入分配onclick事件,则不会发生任何事情。

It sounds like you're trying to submit the form with a traditional submit button (e.g. <input type="submit" value="Cancel">). If that's the case, you may have to use $(yourform).submit()to submit the form, in combination with some handling of the data sent to the server to simulate clicking the Cancel button.

听起来您正在尝试使用传统的提交按钮(例如<input type="submit" value="Cancel">)提交表单。如果是这种情况,您可能不得不使用$(yourform).submit()提交表单,结合对发送到服务器的数据的一些处理来模拟单击取消按钮。

回答by fehays

Is it wrapped in a dom ready event? Might help if you provide more code.

它是否包含在 dom 就绪事件中?如果您提供更多代码,可能会有所帮助。

$(function () {

    $('#XynBp0').find('input').each(function () {
        if ($(this).attr('value') == 'Cancel') {
            $(this).trigger('click');
        }
    });

});

回答by Drew

Your code snippit doesn't make any sense, you are clicking inputs if they are canceled?

您的代码片段没有任何意义,如果它们被取消,您正在单击输入吗?

Here's some things to clean up in your code

这里有一些东西需要清理你的代码

$('input','#XynBp0').each(function () {
  var $this = $(this);
  if ( this.value === 'Cancel' ) { //Don't need jQuery here
    $this.trigger('click');  //Probably don't need it here either
  }
});

What does click even do? If you are trying to submit a form, use form.submit();

点击到底有什么作用?如果您尝试提交表单,请使用 form.submit();