简体   繁体   English

从onSubmit事件处理程序添加或减去函数?

[英]add or subtract functions from onSubmit event handler?

I have the following code: 我有以下代码:

function showAddrBox() {
    var prompt = document.getElementById('addr_prompt');
    prompt.style.display = 'block';
    document.generic_form.address.style.display = 'block';
    document.generic_form.onsubmit = validateAddrForm;
}

function hideAddrBox() {
    var prompt = document.getElementById('addr_prompt');
    prompt.style.display = 'none';
    document.generic_form.address.style.display = 'none';
    document.generic_form.onsubmit = null;
}

The problem is that sometimes I have additional functions attached to onSubmit that I want to preserve. 问题是,有时候我想要保留onSubmit附加的附加功能。 I want to be able to add and remove individual functions from the onSubmit event, not just set them with "onsubmit =". 我希望能够在onSubmit事件中添加和删除单个函数,而不只是使用“onsubmit =”设置它们。 In other words, I need a way to accomplish something like this: 换句话说,我需要一种方法来完成这样的事情:

document.form.onsubmit += function;
document.form.onsubmit -= function;

Any ideas? 有任何想法吗?

Quirksmode has a wonderful article about advanced event registration . Quirksmode有一篇关于高级事件注册精彩文章

Short form is: You can bind multiple events using addEventListener ( attachEvent in older versions of IE). 简写形式是:您可以使用addEventListener (旧版IE中的attachEvent绑定多个事件。

if (someform.addEventListener) {
  someform.addEventListener('submit', somefunc, false);
} else {
  someform.attachEvent('onsubmit', somefunc);
}

To remove them you can use removeEventListener and detachEvent respectively. 要删除它们,可以分别使用removeEventListenerdetachEvent


Pretty quickly you'll get annoyed by repetitively typing addEventListener and attachEvent , and you might consider making some generic code to bind events for you. 很快就会因重复输入addEventListenerattachEvent而烦恼,你可能会考虑制作一些通用代码来为你绑定事件。 Fortunately, other programmers have had the same idea, and many libraries are available that handle event management elegantly. 幸运的是,其他程序员也有同样的想法,并且有许多库可以优雅地处理事件管理。 jQuery tends to be the library of choice, because binding an event is as simple as: jQuery往往是首选的库,因为绑定事件非常简单:

$('#formid').submit(somefunc);

the generic event binding method is: 通用事件绑定方法是:

$('#formid').on('submit', somefunc);

to unbind you can use: 取消绑定你可以使用:

$('#formid').off('submit', somefunc);

Although all of this is well documented in the jQuery API . 虽然所有这些都在jQuery API中有详细记录

Use element.addEventListener("eventName", callbackFunction, false) and element.removeEventListener("eventName", callbackFunction) . 使用element.addEventListener("eventName", callbackFunction, false)element.removeEventListener("eventName", callbackFunction)

Where eventName is the name of the handler without the 'on'. 其中eventName是没有'on'的处理程序的名称。 So onsubmit becomes submit. 所以onsubmit变成提交。

For documentation of the two functions, see: 有关这两个函数的文档,请参阅:

What I would do is have one onSubmit function right on the form that orchestrates the rest of the functions, performs logic on what to do when. 我要做的是在表单上有一个onSubmit函数,它编排其余的函数,执行什么时候做什么的逻辑。 At the end of that function execution you can return true if you want to proceed with the submission or return false if you don't. 在该函数执行结束时,如果要继续提交,则返回true,否则返回false。

u can mention two or more functions for a form like below. 你可以为下面的表格提及两个或更多功能。

<form name="formaname" onsubmit="function1(),function2()">

more on onsubmit 更多关于onsubmit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM