2013-03-27 2 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 = $("[id$='123']", $("input[id*='xyz']")).val(); 

var x = $("input[id*='xyz']").$("[id$='123']").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