2012-05-02 43 views
0
$('select').change(function() { 
    //important stuff 
}); 

验证选择ID做important stuff我想通过它的ID来验证问题的选择之前。我想要影响的所有ID都以_stat结尾。你如何用JQuery中的正则表达式(或其他)做到这一点?JQuery的上变化

$('select').change(function() { 
    if ($("select[id*='_stat']")) { //<~ this aint work 
    //important stuff 
    } 
}); 

回答

3

你可以用if ($(this).is("select[id$='_stat']"))做到这一点,但它肯定会是更好的附加事件处理这些元素只有在首位 - 那么你就不必在所有检查:

$("select[id$='_stat']").change(function() { 
    //important stuff 
}); 
+0

大点。我打算在自己添加这个。 :) +1 –

+0

我会怎么做,乔恩? – Norse

+0

@Norse:更新了解释的答案。 – Jon

1

您正在寻找这样的:

if($(this).is('[id$="_stat"]')) { 
    // important stuff 
} 

但如果你只希望change处理程序与IDS那些select元素,在结束时运行,你应该看看乔恩的答案。

1

你甚至可以只重视与ID在_stat结局选择:

$('select[id$="_stat"]').change(function() { 
    //important stuff only for selects id ending in _stat 
});