【问题标题】:trigger a button after clicking another button from another page with javascript or jquery使用 javascript 或 jquery 从另一个页面单击另一个按钮后触发一个按钮
【发布时间】:2022-01-21 21:41:15
【问题描述】:

我在“home.php”页面上有 3 个按钮,当我点击其中任何一个按钮时,这 3 个按钮会重定向到同一个页面(“create.php”页面),但每个按钮都会自动触发它的新重定向页面(“create.php”页面)中的相应按钮

e.g => home.php 页面中的按钮 1 (id="buttonHome1") 重定向到 create.php 页面并触发 create.php 页面中的按钮 1 (id="buttonCreate1")

按钮 1

<a href="create?car=1">
<button id="buttonHome1">Button 1</button>
</a>

按钮 2

<a href="create?bike=1">
    <button id="buttonHome2">Button 2</button>
</a>

按钮 3

<a href="create?bike=1">
<button id="buttonHome3">Button 3</button>
</a>

在我的 .js 文件中,我得到了这个

var url = window.location.href;
 
window.onload = function () {
    var link = url.split("?car=");
    if (link.length > 1) {
        $("#buttonCreate1").click();
    }
}

此 javascript (jquery) 代码运行良好,但仅针对一个按钮,我的意思是:如果我对 button2 使用相同的 window.onload 函数,运行正常但覆盖第一个按钮,如果用于第三个按钮,覆盖其他 2 个按钮

我希望所有按钮都能正常工作,但我目前不知道如何处理。

我也试过了

$("#buttonHome1").click(function(){
    $("buttonCreate1").click(); 
    return false;
});

【问题讨论】:

  • 您确实应该制定一种可以处理所有情况的方法。没有理由添加多个事件,

标签: javascript php jquery


【解决方案1】:

你试过addEventListener吗?

你可以有多个这样的监听器

window.addEventListener('load', function(){
  // Do your stuff here
});

window.addEventListener('load', function(){
  // Another one
});

https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event

【讨论】:

  • 你是男人 n1md7
猜你喜欢
  • 2011-04-03
  • 2014-04-24
  • 2011-11-23
  • 2020-08-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多