问题描述
如何在按钮点击事件中调用多个函数?
这是我的按钮,
< asp:LinkButton
ID =LbSearch
runat =server
CssClass =regular
onclick =LbSearch_Click
OnClientClick =return validateView(); ShowDiv1();>
但是我的 ShowDiv1
没有被调用...
我的JavaScript函数:
函数ShowDiv1() {
document.getElementById(ReportDiv)。style.display ='block';
返回false;
函数validateView(){
if(document.getElementById(ctl00_ContentPlaceHolder1_DLCategory)。selectedIndex == 0){
document.getElementById (ctl00_ContentPlaceHolder1_ErrorMsg)。innerHTML =请选择您的类别;
document.getElementById(ctl00_ContentPlaceHolder1_DLCategory)。focus();
返回false;
if(document.getElementById(ctl00_ContentPlaceHolder1_DLEmpName)。selectedIndex == 0){
document.getElementById(ctl00_ContentPlaceHolder1_ErrorMsg)。innerHTML =请选择您的员工姓名;
document.getElementById(ctl00_ContentPlaceHolder1_DLEmpName)。focus();
返回false;
}
返回true;如果 ValidateView()
c>函数返回true我应该调用 ShowDiv1()
。
因为你从第一个方法调用返回,所以第二个方法不会执行。
尝试类似于
OnClientClick =var b = validateView(); ShowDiv1(); return b
或扭转情况,
OnClientClick =ShowDiv1(); return validateView();
或者在验证例程中是否存在div1的依赖关系。
OnClientClick =var b = validateView(); if(b)ShowDiv1(); return b
pre>
最好的办法是将多个内联语句封装到像这样的迷你函数中,以简化调用:
//改变逻辑以适应味道
函数clicked(){
var b = validateView();
if(b)
ShowDiv1()
return b;
}
然后
OnClientClick =return clicked();
How to call multiple functions on button click event?
Here is my button,
<asp:LinkButton ID="LbSearch" runat="server" CssClass="regular" onclick="LbSearch_Click" OnClientClick="return validateView();ShowDiv1();">
But my
ShowDiv1
doesn't get called...My JavaScript functions:
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; } return true; }
If the
ValidateView()
function returns true I should callShowDiv1()
.解决方案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();"
这篇关于通过单击按钮调用多个JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!