2010-01-14 1 views
9

내가 모든 드롭을 통해 루프를 원하는 특정 클래스 이름을 선택하고 여기에 항목을 추가하고 난 그냥 올바른 선택으로 고민하고특정 클래스 이름으로 모든 선택 드롭 다운을 가져 오는 올바른 jquery 선택기는 무엇입니까?


편집 : 나는 대부분으로 뭔가 잘못하고 있어야합니다 upvoted 받아 들여진 응답은 일하지 않는다 그래서 나는 나의 부호에있는 약간 변칙이이어야한다 것을 생각한다. 아래 HTML과 jquery 코드를 모두 붙여 넣었습니다. 이 말이 맞는다면 알려주십시오.


HTML :

<select onfocus="this.enteredText='';" onkeydown="return handleKey();" onkeyup="event.cancelbubble=true;return false;" onkeypress = "return selectItem();" class="componentSelect" id="components0" name="applicationUpdater.dependencies[0].componentName" > 
<option value= 5 >Client</option> 
<option value= 79 >Server</option> 
</select> 

<select onfocus="this.enteredText='';" onkeydown="return handleKey();" onkeyup="event.cancelbubble=true;return false;" onkeypress = "return selectItem();" class="componentSelect" id="components1" name="applicationUpdater.dependencies[0].componentName" > 
<option value= 5 >Client</option> 
<option value= 79 >Server</option> 
</select> 

등. . .

jQuery 코드는 :

$('select.componentSelect').each(function() { 
     var select = $(this); 
     $(select).children('option').each(function() { 
      if ($(this).text() == currentComponentName) { 
       $(this).remove(); 
      } 
     }); 

    }); 

답변

6

새 질문에 대답하려면 포함 된 모든 <option>currentComponentName 제거하려면 다음 줄을 사용할 수 있습니다 :

11

당신은 .class selector을 사용해야합니다.

// select every <select> element with classname "yourclassname" 
$('select.yourclassname').each(function() { 
    // $(this) now refers to one specific <select> element 
    // we append an option to it, like you asked for 
    $(this).append(
     $('<option value="foo">Bar</option>'); 
    ); 
}); 

가 제대로 jQuery로 요소를 선택하는 방법에 대한 자세한 내용은 http://docs.jquery.com/Selectors를보십시오.

1

는이처럼 .className 선택기를 사용해야합니다

$('select.className') 

이 선택은 찾을합니다 (className 클래스를 포함하는 모든 요소를 ​​일치) 클래스 명 셀렉터 (모든 <select> 요소를 일치)를 element 선택을 결합 모든 클래스는 <select>입니다.

$('select.className').each(function() { 
    var currentSelect = $(this); 
    // ... do your thing ;-) 
}); 
+0

왜 이것을 다운 그레이드 했습니까? – SLaks

1

이에게 탄 부여

$('select.componentSelect option:contains("' + currentComponentName + '")').remove(); 

Demo

1

당신의 게시 된 jQuery 코드는 currentComponentName을 선택 옵션 중 하나로 정의하는 한 저에게 효과적입니다. Aron Rotteveel의 대답은 매우 가깝지만 추가 부분은 조금 벗어났습니다. 다음을 시도해보십시오.

$('select.componentSelect').each(function() { 
    $(this).append('<option value="foo">Bar</option>'); 
});