简体   繁体   English

按钮不点击不动态点击 - javascript

[英]button not clicking not clicking dynamically - javascript

I am attempting to click a button dynamically using javascript to call a js function, when I launch the page, the javascript function is never called by the button to be clicked dynamically. I am attempting to click a button dynamically using javascript to call a js function, when I launch the page, the javascript function is never called by the button to be clicked dynamically. here is my snippet这是我的片段

<button id="deSubmit" type="submit" >
    To be clicked automatically
</button>

<script type="text/javascript">
    document.getElementById("deSubmit").click();
</script>

Here is the js function I want to be called dynamically这是我想动态调用的js function

$("#deSubmit").click(function () {
    $(function () {
        url_redirect({
            url: "${url}",
            method: "post"
        });
    });
........

Please what could be wrong请问有什么问题

Call it in document.readydocument.ready中调用它

 $("#deSubmit").click(function () { console.log("teste") /*$(function () { url_redirect({ url: "${url}", method: "post" }); });*/ }); $( document ).ready(function() { document.getElementById("deSubmit").click(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="deSubmit" type="submit" >To be clicked automatically</button>

Attach click event inside document.ready .document.ready中附加点击事件。 Also $(function () { this wrapper is not required另外$(function () {这个包装不是必需的

 $(document).ready(function() { document.getElementById("deSubmit").click(); }) $("#deSubmit").click(function() { console.log('test') });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="deSubmit" type="submit">To be clicked automatically</button>

I don't know why you want to click a button, you can write this function on load of script as well.我不知道你为什么要点击一个按钮,你也可以在加载脚本时写这个 function。

You need to enclose your code inside the document ready function to make sure your document loads before any JavaScript code is executed.您需要将您的代码包含在准备好的文档 function 中,以确保在执行任何 JavaScript 代码之前加载您的文档。

And for simplicity purpose, I have invoked click event using JQuery onlu.为简单起见,我使用 JQuery onlu 调用了点击事件。

 $(document).ready(function() { $("#deSubmit").click(function() { /* url_redirect({ url: "${url}", method: "post" }); */ alert('function called'); }); $('#deSubmit').click(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <button id="deSubmit" type="submit"> To be clicked automatically </button>

Don't complicate with jQuery and Javascript.不要与 jQuery 和 Javascript 复杂化。

  <script type="text/javascript">
    document.addEventListener("DOMContentLoaded", function() {

        const btn = document.getElementById("deSubmit");

        btn.addEventListener("click", function() {
          alert("here2")
        });

        btn.click();
    });
  </script>

Use above code this will work.. just put your code in place of the alert.使用上面的代码这将起作用..只需将您的代码放在警报的位置。

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

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