2012-05-02 42 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 
});