简体   繁体   中英

jQuery multiple events and multiple selectors

Is it possible to use multiple selectors and multiple events in jQuery?

I want to combine the following:

$('#submit-modal').click(function() {
$('#modal-form').submit(function() {

Into something like (or similar):

 
 
 
 
  
  
  $('#submit-modal, #modal-form').on('click', 'submit', function() {
 
 
  

I've tried this, but it is not working.

Update (more of what I'm looking to accomplish):

$('#submit-modal').click().$('#modal-form').submit(function() {

I only want the click() attached to the #submit-modal and submit() attached to the #modal-form but if either is initiated it runs the same function.

You can do this:

$("#submit-modal, #modal-form").on("click submit", function() {..});

This made that the anonimous function executes when you click or submit in #submit-modal or #modal-form

The solution here is to trigger the form submission from the click event so that the code related to form submission is present only in one palce.

$('#submit-modal').click(function() {
    $('#modal-form').submit();// $('#modal-form')[0].submit();
});
$('#modal-form').submit(function() {
    //do the submit code here
});

是的,你可能最好用每个选择器保持每个事件,除非它们由同一个事件触发。

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