Calling multiple JavaScript functions on a button click

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

How to call multiple functions on button click event?

Here is my button,



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

    And of course you can never call the two functions at the same time. Not any one in the world except you are working on two processors simultaneously.

    The best way is to call a JavaScript parent function and in that, specify all the sequence of function you want to call. For example,

    function ShowDiv1() {
        document.getElementById("ReportDiv").style.display = 'block';
        return false;
    }
    
    function validateView()
    {
        if (document.getElementById("ctl00_ContentPlaceHolder1_DLCategory").selectedIndex == 0) {
            document.getElementById("ctl00_ContentPlaceHolder1_ErrorMsg").innerHTML = "Please Select Your Category";
            document.getElementById("ctl00_ContentPlaceHolder1_DLCategory").focus();
            return false;
        }
        if (document.getElementById("ctl00_ContentPlaceHolder1_DLEmpName").selectedIndex == 0) {
            document.getElementById("ctl00_ContentPlaceHolder1_ErrorMsg").innerHTML = "Please Select Your Employee Name";
            document.getElementById("ctl00_ContentPlaceHolder1_DLEmpName").focus();
            return false;
        }
        ShowDiv1();
        return true;
    }
    
    0 讨论(0)
  • 2020-12-23 23:32

    That's because, it gets returned after validateView();;

    Use this:

    OnClientClick="var ret = validateView();ShowDiv1(); return ret;"
    
    0 讨论(0)
  • 2020-12-23 23:40

    Because you're returning from the first method call, the second doesn't execute.

    Try something like

    OnClientClick="var b = validateView();ShowDiv1(); return b"
    

    or reverse the situation,

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

    or if there is a dependency of div1 on the validation routine.

    OnClientClick="var b = validateView(); if (b) ShowDiv1(); return b"
    

    What might be best is to encapsulate multiple inline statements into a mini function like so, to simplify the call:

    // change logic to suit taste
    function clicked()  {
        var b = validateView(); 
        if (b) 
            ShowDiv1()
        return b;
    }
    

    and then

    OnClientClick="return clicked();"
    
    0 讨论(0)
  • 2020-12-23 23:41

    Try this .... I got it... onClientClick="var b=validateView();if(b) var b=ShowDiv1();return b;"

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

    if there are more than 2 js function then following two ways can also be implemented:

    1. if you have more than two functions you can group them in if condition

      OR

    2. you can write two different if conditions.

    1 OnClientClick="var b = validateView(); if (b) ShowDiv1(); if(b) testfunction(); return b">

    OR

    2 OnClientClick="var b = validateView(); if(b) {

    ShowDiv1();

    testfunction();

    } return b">

    0 讨论(0)
提交回复
热议问题