博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OAF 使用 javascript 使某个按钮在5秒内不能重复点击
阅读量:7201 次
发布时间:2019-06-29

本文共 5321 字,大约阅读时间需要 17 分钟。

首先要保证按钮是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) { \n" +
" console.log('function start evt ' + evt);\n" +
" disabledButton(); \n" +
" console.log('evt execute');\n" +
" MyPeriodicalExecuter(); \n" +
" } \n" +
" function disabledButton(){ \n" +
" var text = document.getElementById(\"Test\");\n" + //此处的ID改为你要在5秒后启用的ID
" console.log('Test');\n" +
" text.disabled=true;\n" +
" } \n" +
" \n" +
" function MyPeriodicalExecuter(){ \n" +
" succ.loop=0; \n" +
" console.log('111');\n" +
" sh=setInterval(succ,1000); \n" +
" console.log('777');\n" +
" } \n" +
" \n" +
" function succ(){ \n" +
" console.log('222');\n" +
" with(arguments.callee){ \n" +
" console.log('333');\n" +
" console.log('loop = '+loop);\n" +
" loop++;\n" +
" //obj.value=str+\"(\"+(loop++)+\"/15)retry\"; \n" +
" if (loop > 5 ){ \n" +
" console.log('444');\n" +
" enabledButton(); \n" +
" console.log('555');\n" +
" clearInterval(sh); \n" +
" console.log('666');\n" +
" return; \n" +
" } \n" +
" } \n" +
" } \n" +
" \n" +
" function enabledButton(){ \n" +
" console.log('enabledButton evt End');\n" +
" var text = document.getElementById(\"Test\");\n" + //此处的ID改为你要在5秒后启用的ID
" console.log('text getElementById FinSign');\n" +
" text.disabled=false; \n" +
" }";

/**   * 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';\n" + //JavaScript全局变量

//全局变量所带来的 bug 问题非常多,可能很偶然的把代码放到其他页面里,把新页面的同名变量给覆盖,造成隐藏的 bug 。最好尽量少用全局变量。

"function submitApprove(evt) { \n" +
" buttonId = evt;\n" +
" disabledButton(buttonId); \n" +
" MyPeriodicalExecuter(); \n" +
" } \n" +
" function disabledButton(){ \n" +
" var text = document.getElementById(buttonId);\n" + //此处的ID改为你要在5秒后启用的ID
" text.disabled=true;\n" +
" } \n" +
" \n" +
" function MyPeriodicalExecuter(){ \n" +
" succ.loop=0; \n" +
" sh=setInterval(succ,1000); \n" +
" } \n" +
" \n" +
" function succ(){ \n" +
" with(arguments.callee){ \n" +
" loop++;\n" +
" if (loop > 5 ){ \n" +
" enabledButton(); \n" +
" clearInterval(sh); \n" +
" return; \n" +
" } \n" +
" } \n" +
" } \n" +
" \n" +
" function enabledButton(){ \n" +
" var text = document.getElementById(buttonId);\n" + //此处的ID改为你要在5秒后启用的ID
" text.disabled=false; \n" +
" return; \n" +
" }";

 

/**

* 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输出的日志如下

转载地址:http://ogwum.baihongyu.com/

你可能感兴趣的文章
sed批量替换文件内容
查看>>
linux基础命令学习之mkdir(3)
查看>>
ELK体系大型日志分析集群方案设计.搭建.调优.管理
查看>>
cacti监控工具之自定数据收集方法
查看>>
面试感悟----一名3年工作经验的程序员应该具备的技能
查看>>
Windows Phone 8.1 Update 2(GDR2)更新亮点介绍
查看>>
Docker images
查看>>
7 天玩转 ASP.NET MVC — 第 5 天
查看>>
用了OneAPM CT,宕机早知道!
查看>>
程序员:如何接手垃圾代码?
查看>>
获取页面中任意一个元素距离body的偏移量
查看>>
使用POI读写word doc文件
查看>>
rsycn相关知识
查看>>
OpenJudge 4146:数字方格 java穷举法
查看>>
搭建linuxFTP的步骤
查看>>
跟我一起学docker(八)--Dockerfile
查看>>
做SEO,选择大于努力
查看>>
php实现下载文件(浏览器不直接打开)
查看>>
数据库监听卡住 Oracle lsnrctl status 卡在connecting ......
查看>>
python web开发-flask调试模式
查看>>