Calling multiple JavaScript functions on a button click

前端 未结 11 1504
灰色年华
灰色年华 2020-12-23 22:37

How to call multiple functions on button click event?

Here is my button,



        
相关标签:
11条回答
  • 2020-12-23 23:20

    Change

    OnClientClick="return validateView();ShowDiv1();">
    

    to

    OnClientClick="javascript: if(validateView()) ShowDiv1();">
    
    0 讨论(0)
  • 2020-12-23 23:23

    It isn't getting called because you have a return statement above it. In the following code:

    function test(){
      return 1;
      doStuff();
    }
    

    doStuff() will never be called. What I would suggest is writing a wrapper function

    function wrapper(){
       if (validateView()){
         showDiv();
         return true;
       }
    }
    

    and then call the wrapper function from your onclick handler.

    0 讨论(0)
  • 2020-12-23 23:24
      <asp:Button ID="btnSubmit" runat="server"  OnClientClick ="showDiv()"
       OnClick="btnImport_Click" Text="Upload" ></asp:Button>
    
    0 讨论(0)
  • 2020-12-23 23:28

    I think that since return validateView(); will return a value (to the click event?), your second call ShowDiv1(); will not get called.

    You can always wrap multiple function calls in another function, i.e.

    <asp:LinkButton OnClientClick="return display();">
    
    function display() {
       if(validateView() && ShowDiv1()) return true;
    }
    
    

    You also might try:

    <asp:LinkButton OnClientClick="return (validateView() && ShowDiv1());">
    

    Though I have no idea if that would throw an exception.

    0 讨论(0)
  • 2020-12-23 23:29

    At times it gives syntax error that "return is not a function" so in that case just remove return and it will work fine :) as shown below

    OnClientClick="var b = validateView(); if(b) var b = ShowDiv1(); b;"
    
    0 讨论(0)
  • 2020-12-23 23:30
    btnSAVE.Attributes.Add("OnClick", "var b = DropDownValidate('" + drp_compcode.ClientID + "') ;if (b) b=DropDownValidate('" + drp_divcode.ClientID + "');return b");
    
    0 讨论(0)
提交回复
热议问题