【发布时间】: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