JQuery / JavaScript - trigger button click from an

2019-01-21 22:07发布

I have this HTML which looks like this:

<input type="submit" name="savebutton" class="first button" />
<input type="submit" name="savebutton" class="second button" />

and 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...

5条回答
聊天终结者
2楼-- · 2019-01-21 22:47
jQuery("input.first").click(function(){
   jQuery("input.second").trigger("click");
   return false;
});
查看更多
走好不送
3楼-- · 2019-01-21 22:52

this works fine, but file name does not display anymore.

$(document).ready(function(){ $("img.attach2").click(function(){ $("input.attach1").click(); return false; }); });

查看更多
Lonely孤独者°
4楼-- · 2019-01-21 22:54

Add id's to both inputs, id="first" and id="second"

//trigger second button
$("#second").click()
查看更多
乱世女痞
5楼-- · 2019-01-21 23:00

You mean this:

jQuery("input.first").click(function(){
   jQuery("input.second").trigger('click');
   return false;
});
查看更多
虎瘦雄心在
6楼-- · 2019-01-21 23:12

Well, you just fire the desired click event:

$(".first").click(function(){
    $(".second").click(); 
    return false;
});
查看更多
登录 后发表回答