简体   繁体   English

点击事件未触发-jQuery

[英]Click event is not triggered - jQuery

I have on my local machine something like this: 我在本地计算机上有如下内容:

<form>
...
<input type="submit" name="bouton" id="bouton" style="display:none;" />    
</form>
<script type="text/javascript">
setTimeout(function ()
{
    console.log("test");
    $("#bouton").click();
    //alert("redirection");
}, 150);
</script>

When this page is loaded the click event is correctly triggered. 加载此页面后,将正确触发click事件。 Now, on my remote server the exact same code does not trigger this event but the console.log message works! 现在,在我的远程服务器上,完全相同的代码不会触发此事件,但是console.log消息有效! I tried as suggested by some SO post to add $("#bouton")[0].click(); 我尝试按照一些SO帖子的建议添加$("#bouton")[0].click(); but it didn't work. 但这没用。 How can it be? 怎么可能?

Wrap your entire code in this call: 在此调用中包装整个代码:

$(document).ready(function(){
// your code here
});

Most likely, the same explanation I did here is valid too: jquery click event not fired on internet explorer 最有可能的是,我在这里所做的相同解释也是有效的: Internet Explorer上未触发jquery click事件

Please read the entire answer: it contains some information to help you debug your code. 请阅读完整的答案:它包含一些信息来帮助您调试代码。

Jsfiddle of working code . 工作代码

$(function() {
  setTimeout(function () {
  console.log("test");
  $("#bouton").click();
   //alert("redirection");
  }, 2000);
});

Do check in http://jsfiddle.net/7M8Gj/ 请检查http://jsfiddle.net/7M8Gj/

maybe jQuery is not loaded yet, try this to ensure 可能尚未加载jQuery ,请尝试执行此操作以确保

var l = document.getElementById("bouton");
l.click();

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

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