JQuery / JavaScript - 从另一个按钮单击事件触发按钮单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3560227/
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
JQuery / JavaScript - trigger button click from another button click event
提问by Alex
I have this HTML which looks like this:
我有这个 HTML 看起来像这样:
<input type="submit" name="savebutton" class="first button" />
<input type="submit" name="savebutton" class="second button" />
and JS:
和JS:
jQuery("input.second").click(function(){
// trigger second button ?
return false;
});
So how can I trigger the click event from the 2nd button by clicking on the first one? Note that I don't have any control over the 2nd button, neither on the html or the click event on it...
那么如何通过单击第一个按钮从第二个按钮触发单击事件?请注意,我对第二个按钮没有任何控制权,无论是 html 还是它的点击事件...
回答by cichy
Add id's to both inputs, id="first" and id="second"
将 id 添加到两个输入,id="first" 和 id="second"
//trigger second button
$("#second").click()
回答by robertbasic
Well, you just fire the desired click event:
好吧,您只需触发所需的点击事件:
$(".first").click(function(){
$(".second").click();
return false;
});
回答by pulsedemon
jQuery("input.first").click(function(){
jQuery("input.second").trigger("click");
return false;
});
回答by Sarfraz
You mean this:
你是这个意思:
jQuery("input.first").click(function(){
jQuery("input.second").trigger('click');
return false;
});
回答by Galus
this works fine, but file name does not display anymore.
这工作正常,但文件名不再显示。
$(document).ready(function(){ $("img.attach2").click(function(){ $("input.attach1").click(); return false; }); });
回答by Svibhor
By using JavaScript: document.getElementById("myBtn").click();
通过使用 JavaScript: document.getElementById("myBtn").click();
回答by Black
If it does not work by using the click()method like suggested in the accepted answer, then you can try this:
如果使用已click()接受的答案中建议的方法不起作用,那么您可以尝试以下操作:
//trigger second button
$("#second").mousedown();
$("#second").mouseup();

