如何将事件附加到表单的 onSubmit 事件并链接早期附加的方法?

发布于 2024-08-08 22:13:08 字数 1309 浏览 9 评论 0原文

我的申请有数百页。现在我必须在表单的 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() 已附加到所有页面的表单中。

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

请不要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯℡ 2024-08-15 22:13:08

根据 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.

云朵有点甜 2024-08-15 22:13:08

请注意,虽然接受的答案是正确的(添加了事件处理程序,但它不会替换现有的事件处理程序),但它可能不是用户所期望的:

您的事件处理程序可以以任何顺序与其他事件处理程序一起调用。并且 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文