2013-03-27 60 views
2

我有相同類型的多個控件,用在ID中間「XYZ」:JQuery的選擇

<input type="text" id="AutoStuff_hfyrt_xyz_MoreStuf_123" /> 
    <input type="text" id="AutoStuff_fyhrt_xyz_MoreStuf_7" /> 

如果我想在一個結束的「123,」我想那將是:

var x = $("input[id*='xyz']").("[id$='123']").val() 

但是,我收到一個錯誤,「標識符預期」。

回答

0

嘗試

var x = $("input[id*='xyz']").$("[id$='123']").val(); 

var x = $("[id$='123']", $("input[id*='xyz']")).val(); 

在第二種情況下,你可能需要切換參數或選擇的順序。

0

var x = $('#AutoStuff_hfyrt_xyz_MoreStuf_123')將是選擇器。 []用於屬性,所以我想也許這可能是導致錯誤的原因。

[attr] has attribute 
[attr=val] has attribute with value val 
[attr!=val] does not have attribute with value val 
[attr^=val] attribute begins with val 
[attr$=val] attribute ends with val 
[attr*=val] attribute includes val 
[attr~=val] attribute includes val as a word 
[attr|=val] attribute begins with val and optional hyphen 

來自jquery袖珍指南。

2

您可以使用.filter()或使用Multiple Attribute Selector將兩者合併。

// Using .filter() 
var x = $("input[id*='xyz']").filter("[id$='123']").val(); 

// Using the Multiple Attribute Selector 
var x = $("input[id*='xyz'][id$='123']").val(); 

DEMO