简体   繁体   中英

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(); 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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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