2012-02-03 48 views
3

我試圖使用jQuery選擇是找到具有特定名稱的所有輸入字段:jQuery的屬性選擇用一段

$('input[name=Filter']).click(function() { 
    // do something 
}); 

這應該與以下項目:

<input type="radio" name="Filter" value="A" /> 
<input type="radio" name="Filter" value="B" /> 

這工作正常。我遇到的問題是,我的名字是動態的ASP.net MVC生成的名字有一個時期:

<input type="radio" name="Parent.Filter" value="A" /> 
<input type="radio" name="Parent.Filter" value="B" /> 

我試着寫了以下選擇:

$('input[name=Parent.Filter]').click(function() { 
}); 

我收到以下錯誤: Uncaught Syntax error, unrecognized expression: [name=Parent.Filter]

如何編寫此表達式以使其正常工作?

+1

不知道是否會工作,但嘗試...'$ ('input [name =「Parent.Filter」]')' – musefan 2012-02-03 17:40:38

+0

您是否嘗試過輸入[name * =「Filter」]''? – kei 2012-02-03 17:42:09

回答

8

行情是強制性的,在這種情況下它不會沒有他們的工作。

$('input[name="Parent.Filter"]').click(function() { 
}); 

From the API reference

value An attribute value. Quotes are mandatory.

Here's a jsFiddle演示此。

+0

非常好。萬分感謝。 – Dismissile 2012-02-03 17:43:19

2

這工作,你只需要「」周圍的名稱:

$('input[name="Parent.Filter"]').click(function() { 
}); 
1

根據我的問題,我可以證實,這確實可行評論...

$('input[name="Parent.Filter"]').click(function() { 
}); 

Working example here

0

使用​​3210