add or subtract functions from onSubmit event handler?

后端 未结 4 558
轮回少年
轮回少年 2020-12-21 10:05

I have the following code:

function showAddrBox() {
    var prompt = document.getElementById(\'addr_prompt\');
    prompt.style.display = \'block\';
    docu         


        
相关标签:
4条回答
  • 2020-12-21 10:06

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

    Where eventName is the name of the handler without the 'on'. So onsubmit becomes submit.

    For documentation of the two functions, see:

    • https://developer.mozilla.org/en/DOM/element.addEventListener
    • https://developer.mozilla.org/en/DOM/element.removeEventListener
    0 讨论(0)
  • 2020-12-21 10:15

    Quirksmode has a wonderful article about advanced event registration.

    Short form is: You can bind multiple events using addEventListener (attachEvent in older versions of IE).

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

    To remove them you can use removeEventListener and detachEvent respectively.


    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. 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:

    $('#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.

    0 讨论(0)
  • 2020-12-21 10:16

    u can mention two or more functions for a form like below.

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

    more on onsubmit

    0 讨论(0)
  • 2020-12-21 10:31

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

    0 讨论(0)
提交回复
热议问题