首先要保证按钮是BUTTON,并且按钮事件设置firePartialAction。
public class CuxXXXXPGCO extends OAControllerImpl { public static final String RCS_ID = "$Header$"; public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion(RCS_ID, "%packagename%");
private String disableButtonMultiClickByIdJs ="function submitApprove(evt) {
" +
" console.log('function start evt ' + evt);
" +
" disabledButton();
" +
" console.log('evt execute');
" +
" MyPeriodicalExecuter();
" +
" }
" +
" function disabledButton(){
" +
" var text = document.getElementById("Test");
" + //此处的ID改为你要在5秒后启用的ID
" console.log('Test');
" +
" text.disabled=true;
" +
" }
" +
"
" +
" function MyPeriodicalExecuter(){
" +
" succ.loop=0;
" +
" console.log('111');
" +
" sh=setInterval(succ,1000);
" +
" console.log('777');
" +
" }
" +
"
" +
" function succ(){
" +
" console.log('222');
" +
" with(arguments.callee){
" +
" console.log('333');
" +
" console.log('loop = '+loop);
" +
" loop++;
" +
" //obj.value=str+"("+(loop++)+"/15)retry";
" +
" if (loop > 5 ){
" +
" console.log('444');
" +
" enabledButton();
" +
" console.log('555');
" +
" clearInterval(sh);
" +
" console.log('666');
" +
" return;
" +
" }
" +
" }
" +
" }
" +
"
" +
" function enabledButton(){
" +
" console.log('enabledButton evt End');
" +
" var text = document.getElementById("Test");
" + //此处的ID改为你要在5秒后启用的ID
" console.log('text getElementById FinSign');
" +
" text.disabled=false;
" +
" }";
/** * Layout and page setup logic for a region. * @param pageContext the current OA page context * @param webBean the web bean corresponding to the region */ public void processRequest(OAPageContext pageContext, OAWebBean webBean) { super.processRequest(pageContext, webBean); pageContext.removeJavaScriptFunction("submitApprove"); // pageContext.putJavaScriptFunction("submitApprove", disableButtonMultiClickJs); pageContext.putJavaScriptFunction("submitApprove", disableButtonMultiClickByIdJs); String submitJs = "javascript:submitApprove(this.id);"; OAButtonBean button = (OAButtonBean)webBean.findChildRecursive("Test"); button.setAttributeValue(OAWebBeanConstants.ON_CLICK_ATTR,submitJs); //两个方法是一致的 // button.setOnClick(submitJs); //两个方法是一致的
//your other business logic ...
}
/**
* Procedure to handle form submissions for form elements in
* a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void processFormRequest(OAPageContext pageContext,
OAWebBean webBean)
{
//your business logic
}
}
此方法仍然有一个可以优化的地方,js代码中getElementById中的id值是写死了的,可以其实通过传入button.id的形式,使用全局变量,使得查找button通过id的方式,代码如下
1. 要清楚在哪个地方声明定义的是 全局变量 还是 局部变量 。
2. 全局变量所带来的 bug 问题非常多,可能很偶然的把代码放到其他页面里,把新页面的同名变量给覆盖,造成隐藏的 bug 。最好尽量少用全局变量。
所以这里要使用全局变量还是局部变量就见仁见智了
(不过经过测试,在IE浏览器中,此段代码不能正常运行,导致Test Button中的业务逻辑不能正常执行,猜测可能是Console.log引起的,建议删除Console.log,不然IE浏览器不能正确运行。)
(IE不支持console.log,会阻止程序的进一步执行)
public class CuxXXXXPGCO
extends OAControllerImpl
{
public static final String RCS_ID = "$Header$";
public static final boolean RCS_ID_RECORDED =
VersionInfo.recordClassVersion(RCS_ID, "%packagename%");
//禁用按钮在5秒内连续点击
private String disableButtonMultiClickByIdJs ="var buttonId ='buttonId';
" + //JavaScript全局变量
//全局变量所带来的 bug 问题非常多,可能很偶然的把代码放到其他页面里,把新页面的同名变量给覆盖,造成隐藏的 bug 。最好尽量少用全局变量。
"function submitApprove(evt) {
" +
" buttonId = evt;
" +
" disabledButton(buttonId);
" +
" MyPeriodicalExecuter();
" +
" }
" +
" function disabledButton(){
" +
" var text = document.getElementById(buttonId);
" + //此处的ID改为你要在5秒后启用的ID
" text.disabled=true;
" +
" }
" +
"
" +
" function MyPeriodicalExecuter(){
" +
" succ.loop=0;
" +
" sh=setInterval(succ,1000);
" +
" }
" +
"
" +
" function succ(){
" +
" with(arguments.callee){
" +
" loop++;
" +
" if (loop > 5 ){
" +
" enabledButton();
" +
" clearInterval(sh);
" +
" return;
" +
" }
" +
" }
" +
" }
" +
"
" +
" function enabledButton(){
" +
" var text = document.getElementById(buttonId);
" + //此处的ID改为你要在5秒后启用的ID
" text.disabled=false;
" +
" return;
" +
" }";
/**
* Layout and page setup logic for a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void processRequest(OAPageContext pageContext,
OAWebBean webBean)
{
super.processRequest(pageContext, webBean);
pageContext.removeJavaScriptFunction("submitApprove");
// pageContext.putJavaScriptFunction("submitApprove", disableButtonMultiClickJs);
pageContext.putJavaScriptFunction("submitApprove", disableButtonMultiClickByIdJs);
String submitJs = "javascript:submitApprove(this.id);";
OAButtonBean button = (OAButtonBean)webBean.findChildRecursive("Test");
button.setAttributeValue(OAWebBeanConstants.ON_CLICK_ATTR,submitJs); //与下面的等价
// button.setOnClick(submitJs);//与上面的等价
//
}
/**
* Procedure to handle form submissions for form elements in
* a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void processFormRequest(OAPageContext pageContext,
OAWebBean webBean)
{
//your logic
}
}
同时还有一个未解决的bug,如果在按钮执行之后发生了跳转,通过控制台可以看到loop会一直递增,直到发生下一个事件时终止。
Chrome浏览器在开启了F12日志诊断功能之后,可以看到console.log输出的日志如下