如何通过链接早期附加的方法将事件附加到表单的onSubmit事件? [英] How to attach an event to onSubmit event of form with chaining earlier attached methods as well?

查看:116
本文介绍了如何通过链接早期附加的方法将事件附加到表单的onSubmit事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序实际上有数百页。现在我必须在onSubmit表单上附加一个事件'disablePage'。我不想去每一页并写下:

Actaully my application is having hundreds of pages. Now i have to attach an event 'disablePage' on onSubmit of form. I don't want to go to each and every page and write:

<form name="frmname" onSubmit="disablePage();">

我现在正在做的是: -

What i am doing right now is:-

摘自common.js文件; [包含在所有页面中]

excerpt from common.js file; [ included in all pages ]

/* we have to attach 'attachFormSubmit' method on onLoad event, 
   otherwise forms[0] will always be null. If there is any alternative, 
   then please suggest one */
if (window.addEventListener){   
    window.addEventListener('load', attachFormSubmit, false); 
} else if (window.attachEvent){ 
    window.attachEvent('onload', attachFormSubmit );
}

function attachFormSubmit(){
    forms = document.getElementsByTagName('Form');  
    if ( forms[0] ){  // there is only one form in all pages    
        if (forms[0].addEventListener){         
            forms[0].addEventListener('submit', disablePage, false); 
        } else if (forms[0].attachEvent){           
            forms[0].attachEvent('onsubmit', disablePage);
        }
    }
}

function disablePage(){     
    document.getElementById("pageHideDivID").style.display='inline';        
}

直到这一切一切正常,disablePage()附加到表格中所有页面。

Till this point everything is fine, disablePage() is attached to the forms of all page.

但问题是,如果有人已经将任何方法附加到onSubmit或onLoad,那么也应该执行。我想根据我的代码,代码永远不会被执行。
我该怎样做才能链接他们的方法?

But the problem is, if someone have already attached any method to onSubmit or onLoad then that too should be executed. And i guess according to my code that code will never be executed. What should i do to chain their method too?

请不要JQuery

推荐答案

根据 Quirksmode 你在做什么应该实际工作。它应该只添加您的事件而不是替换旧事件。这就是你使用addEventListener / attachEvent而不是赋值给onsubmit的原因。

According to Quirksmode what you are doing should actually work. It should just add your events and not replace the old ones. That's why you use addEventListener/attachEvent instead of assignment to onsubmit.

这篇关于如何通过链接早期附加的方法将事件附加到表单的onSubmit事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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