如何将事件附加到表单的 onSubmit 事件并链接早期附加的方法?
我的申请有数百页。现在我必须在表单的 onSubmit
上附加一个事件 disablePage
。我不想去每一页并写:
<form name="frmname" onSubmit="disablePage();">
我现在正在做的是: -
摘自 common.js 文件; [包含在所有页面中]
/* 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()
已附加到所有页面的表单中。
但问题是,如果有人已经将任何方法附加到 onSubmit
或 onLoad
,那么也应该执行该方法。我猜根据我的代码,代码永远不会被执行。 我应该怎么做才能链接他们的方法?
请不要使用 JQuery。
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:-
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';
}
Till this point everything is fine, disablePage()
is attached to the forms of all page.
But the problem is, if someone have already attached any method to onSubmit
or onLoad
then that too should be executed. I guess according to my code that code will never be executed.
What should I do to chain their method too?
No JQuery please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 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 处理程序可能会取消向服务器的提交。因此,如果您在提交时“禁用”页面,您可能会将用户留在一个他无法再执行任何操作的页面上......
Note, that while the accepted answer is correct (the event handler is added, it does NOT replace the existing event handler), it might be not what the user expects:
Your event handler may be called in any order with the other event handlers. And onsubmit handlers may cancel the submission to the server. So, if you "disable" the page upon submit, you might leave the user on a page where he cannot do anything any more...