简体   繁体   中英

Jquery on selector for element parent

To use jquery on method instead of deprecated live method in this simple case

$("#myForm").live('submit', function() {
   alert("submit");
});

would be

$(document).on('submit', '#myForm', function() {
  alert("submit");
});

Now, how to do the same with "pure" on , without extra things(like eg assign id to form and then use that id as a selector or smth like that) in this case

$("#myInput").parents("form").live('submit', function() {
   alert("submit");
});

thanks

Run the event handler on all form submissions, then test to see if the form contains the input you care about inside it.

jQuery(document).on('submit', 'form', function (evt) {
    if (jQuery(evt.target).find('#myInput').length === 0) {
        return;
    }
    // Otherwise run the rest of the function normally
});

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