你能幫我簡化這個選擇器嗎?反正有沒有簡化我的jQuery Selector代碼?
下面是代碼:
$("input[name='grpQuestion1'], input[name='grpQuestion2'], input[name='grpQuestion4']").change(function (event) {
/* Some code here */
});
你能幫我簡化這個選擇器嗎?反正有沒有簡化我的jQuery Selector代碼?
下面是代碼:
$("input[name='grpQuestion1'], input[name='grpQuestion2'], input[name='grpQuestion4']").change(function (event) {
/* Some code here */
});
你的,你需要明確的三個名字的評論說,這樣
var names = ['grpQuestion1', 'grpQuestion2', 'grpQuestion4'];
$.each(names, function(i, v) {
$('input[name='+v+']').change(function (event) {
/* Some Code Here */
});
});
會使它更容易維護。
我有什麼用? –
'i'是一個鍵(整數,例如:0,1,2,...),'v'是數組的值(對象類型如:grpQuestion1,grpQuestion2,...) –
非常感謝。 –
嘗試這個
$('input[name^="grpQuestion"]').change(function (event) { /* Some Code Here */ });
jQuery有一些屬性選擇
等,其屬性與值的單詞開始$("[attribue^=value]")
元件。 $("[attribue$=value]")
屬性以值字結尾的元素。 $("[attribue*=value]")
元素屬性包含值作爲一個部分或一個單詞或確切的單詞。該屬性包含的值作爲確切的詞 $("[attribue~=value]")
元素
我需要的名字是專門grpQuestion1,grpQuestion2,grpQuestion4只。 –
謝謝你的幫助。我會試着嘗試一下這個。 –
$('input[name^="grpQuestion"]').change(/** your code */)
它所做的基本上是選擇以「grpQuestion」開始的所有元素
試試這個
$("input[name^=grpQuestion]").change(function (event) {
/* Some Code Here */ });
要選擇有name屬性與「grpQuestion」開頭的第4個輸入單元,你可以使用下面的jQuery選擇:
$('input[name^="grpQuestion"]:lt(4)')
你需要在你的選擇器周圍使用單引號(或者圍繞着名字,無論哪種方式) - 我知道這不是問題,只是說... – m7o