如何在单个onclientclick事件中调用两个脚本 [英] how to call two script in single onclientclick event

查看:90
本文介绍了如何在单个onclientclick事件中调用两个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这里我想在单个onclientclick事件中调用两个javascript两个是不同的脚本,一个是普通脚本,另一个是返回(真或假)。



代码如下:



hi to all,
Here i want to call two javascript in single onclientclick event two are different scripts one is normal script and another one is return(true or false).

code is attached below:

<asp:Button ID="cmdSave" ValidationGroup="purchaseval" runat="server" Text="" CssClass="savebutton1231" OnClientClick="ClientSideClick(this);return ConfirmNEW();"
                                                                                                               EnableTheming="false" OnClick="cmdSave_Click" SkinID="skinBtnSave" />





以上按钮不开火cmdsave_click.bcz一个脚本想要返回而另一个是正常脚本如何区分这两个脚本



这是我的脚本: -

函数ClientSideClick(myButton){



// if(typeof(Page_ClientValidate)=='function')

{

// var isPageValid = true;



var isGrpOneValid = Page_ClientValidate(purchaseval);



if(isGrpOneValid == true){

if (myButton.getAttribute('type')=='button')

{

alert(isGrpOneValid);

myButton.disabled = true ;

myButton.className =btn-inactive;

myButton.value =处理......;



}

}

}

}

以上脚本多次点击避免





above button not fire cmdsave_click.bcz one script want to return and another is a normal script how to differentiate this two scripts

this is my scriptS:-
function ClientSideClick(myButton) {

// if (typeof (Page_ClientValidate) == 'function')
{
// var isPageValid = true;

var isGrpOneValid = Page_ClientValidate("purchaseval");

if (isGrpOneValid == true) {
if (myButton.getAttribute('type') == 'button')
{
alert(isGrpOneValid);
myButton.disabled = true;
myButton.className = "btn-inactive";
myButton.value = "processing...";

}
}
}
}
above script for multiple click avoid

function ConfirmNEW() {

           if (Page_ClientValidate()) {

               var GridId = "<%=grvStudentDetails.ClientID %>";
               var grid = document.getElementById(GridId);
               rowscount = grid.rows.length;

               // var row = drpPrd.parentNode.parentNode;
               // var rowIndex = row.rowIndex - 1;
               // for (var i = rowIndex; i <= rowIndex; i++) {

               for (var i = 1; i < rowscount; i++) {
                   //alert(i);
                   var indexID = 01 + i;

                   var vatch = document.getElementById('ctl00_cplhControlPanel_tabs2_TabPanel2_grvStudentDetails_ctl' + ((indexID < 10) ? '0' + indexID.toString() : indexID.toString()) + '_txtVATPre').value;
                   var cstch = document.getElementById('ctl00_cplhControlPanel_tabs2_TabPanel2_grvStudentDetails_ctl' + ((indexID < 10) ? '0' + indexID.toString() : indexID.toString()) + '_txtCSTPre').value;
                   //alert(vatch);
                   __doPostBack('cmdSave', "")
                   if ((vatch != "" && vatch <= 0) && (cstch != "" && cstch <= 0)) {
                       if (confirm("VAT(%) and CST(%) are Zeros(0) in row" + i + ".Do you want to Continue?")) {

                           return true;
                       }
                       else {

                           return false;
                       }
                   }
               }
           }
       }





这个用于确认目的。



this for confirm purpose.

推荐答案

Hello,

In ASP.net,It Executes ClientSide methods first and then it'll call server side methods.

If function ConfirmNEW() will return false then it will not execute cmdSave server side button click event but You wan't to Execute cmdSave at Server side then use __doPostBack() Event.

 __doPostBack('cmdSave', '');  within function ConfirmNEW() before return true or false.


如果你想执行服务器端按钮点击事件,不管第二个javascript函数,即ConfirmNEW()返回是或否,然后将您的代码更改为以下。



删除退货声明



Ff you want to execute server side button click event irrespective of whether 2nd javascript function i.e. ConfirmNEW() returns true or false, then change your code to below.

remove return statement

<asp:button id="cmdSave" validationgroup="purchaseval" runat="server" text="" cssclass="savebutton1231" onclientclick="ClientSideClick(this);ConfirmNEW();" xmlns:asp="#unknown">
                                                                                                                   EnableTheming="false" OnClick="cmdSave_Click" SkinID="skinBtnSave" /></asp:button>


您可以在包装函数中聚合您的函数并使它们全部自动执行,如下面的脚本示例:



[< a href =https://jsbin.com/yujige/edit?html,js,consoletarget =_ blank> ^ ]



You can aggregate your functions inside wrapper function and make them all self executing, like the script example below:

[^]

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">

    <script type='text/javascript'>
      function go()
      {
        var result;

        console.log('in');

        // first call
        (function go1(){
           console.log('go 1');
        })();

        // second call, with return
        (function go2(){
           console.log('go 2');

           //set your return value here
           result = false;
        })();

        // just to view the result
        document.getElementById('result').innerHTML = 'Result: '+result;

        //returned value
        return result;
      }
    </script>
  </head>
  <body>
    <a href="http://example.com" onclick="return go();">Link</a>

    <label id="result">
  </body>
</html>





输出:

in

去1

去2

结果:false



如果您将结果更改为true,它将离开以便返回值正常工作



Output:
"in"
"go 1"
"go 2"
Result: false

if you change result to true, it will navigate away so the return value is working


这篇关于如何在单个onclientclick事件中调用两个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆