2015-07-21 58 views
0

我有一个未知数量的不同ID,它看起来像#send_1,#send_2等等。我需要调用一个jQuery的每个ID的,如:如何将jQuery功能与不同选择器结合使用

$(document).on("submit", "#send_1", function(e){ 
    var i=1; 
    if (something is true) { 
     do something 
    }  
}); 
$(document).on("submit", "#send_2", function(e){ 
    var i=1; 
    if (something is true)) { 
     do something 
    }  
}); 

,所以我可以写一百遍一样的功能只是改变_x但应解决这个更propper方式。

我通常不使用jQuery,所以如果有人可以帮助我,我真的很感激。

在此先感谢。

+0

我的回答有一个错字。现在试试 – AmmarCSE

回答

3

使用属性starts with(^ =)选择

$(document).on("submit", '[id^="send_"]', function(e){ 
    var i=1; 
    if (something is true)) { 
     do something 
    }  
}); 
+0

惊人的...... thx这么多:D有一个美好的夜晚。 – John

+0

@John,很高兴帮助:) – AmmarCSE

1

你也可以将它们与 ''

像这样分离:

$(document).on("submit", "#send_1, #send_2", function(e){ 
//... 
}); 
相关问题