简体   繁体   中英

Pure JS. How to add event listener for submit any form

i tried to addEventListener once for all forms on page to serialize and check them before ajax sending. If you have an pill for my broken head, share it pls

window.addEventListener('submit', function(e){

        //Serialize Form
        //Ajax send

    }, false);

thx much

You can query all forms and add event listener to each item:

 document.querySelectorAll('form').forEach(form => form.addEventListener('submit', function(e){ e.preventDefault(); console.log('test'); }, false));
 <form> <button>submit</button> </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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