简体   繁体   English

触发click事件后触发一个函数

[英]fire a function after triggering a click event

How can fix this : 怎么解决这个问题:

function Navigation(sender) {
    var senderID = sender.id;


    var answer = confirm("do you want to save your current layout ?");
    if (answer) {
        $("#loadingImg").css("display", "block");
        $("#<%=Button1.ClientID %>").click();
         //the next line is never fired
         if (senderID == "AboutClick") { setTimeout('ShowLoadingMsg()', 3000); } 

    }    
 }
function ShowLoadingMsg() {
   window.location="About.aspx";
}

<a href="javascript:void(0)" id="AboutClick" class="menu" onclick="Navigation(this);" >Navigate Click</a>
<asp:Button ID="Button1" runat="server" OnClick="btnSaveState_Click" style="display:none;" />

//Server side: //服务器端:

 protected void btnSaveState_Click(object sender, EventArgs e)
{

     SaveState();

}

The main problem is that this line is never fired what am i doing wrong here 主要的问题是这条线永远不会被解雇我在这里做错了什么

The problem here is that $("#<%=Button1.ClientID %>").click(); 这里的问题是$("#<%=Button1.ClientID %>").click(); causes the entire page to reload. 导致整个页面重新加载。 It won't really matter what scripts you set a timeout for after that, since the page is refreshed anyway. 在此之后设置超时的脚本并不重要,因为无论如何页面都会刷新。

You could try putting Button1 inside an UpdatePanel , or just solve the problem in another way, such as saving state and redirecting in the same method. 您可以尝试将Button1放在UpdatePanel中 ,或者只是以其他方式解决问题,例如在同一方法中保存状态和重定向。

Try this: 尝试这个:

<a href="#" id="AboutClick" class="menu trigger">Navigate Click</a>​

- -

$(function(){
    $(".trigger").click(function(){
        var answer = confirm("do you want to save your current layout ?");
        if (answer) {
            $("#loadingImg").show();
            if (this.id == "AboutClick") { setTimeout('ShowLoadingMsg()', 3000); }
            $("#<%=Button1.ClientID %>").click();
        }  
    })       
})

function ShowLoadingMsg() {
   window.location="About.aspx";
}

Demo here! 在这里演示!

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

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