Programing

jQuery로 선택 옵션의 레이블을 얻는 방법은 무엇입니까?

crosscheck 2020. 8. 5. 07:48
반응형

jQuery로 선택 옵션의 레이블을 얻는 방법은 무엇입니까?


<select>
<option value="test">label </option>
</select>

이 값을 검색 할 수 있습니다 $select.val().

무엇에 대해 label?

IE6에서 작동하는 솔루션이 있습니까?


이 시도:

$('select option:selected').text();

안녕하세요 먼저 선택에 ID를 부여하십시오.

<select id=theid>
<option value="test">label </option>
</select>

그런 다음 선택한 레이블을 다음과 같이 호출 할 수 있습니다.

jQuery('#theid option:selected').text()

참고로 label옵션 태그 에는 보조 속성 이 있습니다 .

//returns "GET THIS" when option is selected
$('#selecter :selected').attr('label'); 

HTML

<select id="selecter">
<option value="test" label="GET THIS">
Option (also called label)</option>
</select>

드롭 다운에서 특정 옵션의 레이블을 얻으려면 이것을 입력하십시오.

$('.class_of_dropdown > option[value='value_to_be_searched']').html();

또는

$('#id_of_dropdown > option[value='value_to_be_Searched']').html();

$("select#selectbox option:eq(0)").text()

"option : eq (0)"의 0 인덱스는 검색하려는 인덱스 옵션과 교환 할 수 있습니다.

도움이됩니다 : http://www.myphpetc.com/2009/03/jquery-select-element-cheat-sheet.html


이 시도:

$('select option:selected').prop('label');

두 가지 스타일의 <option>요소에 대해 표시된 텍스트를 가져옵니다 .

  • <option label="foo"><option> -> "foo"
  • <option>bar<option> -> "bar"

label요소 내부에 속성과 텍스트 가 모두있는 label경우 브라우저와 동일한 동작 인 속성을 사용 합니다.

후손을 위해, 이것은 jQuery 3.1.1에서 테스트되었습니다.


도움이 되었음

$('select[name=users] option:selected').text()

this키워드를 사용하여 선택기에 액세스 할 때

$(this).find('option:selected').text()

<SELECT id="sel" onmouseover="alert(this.options[1].text);"
<option value=1>my love</option>
<option value=2>for u</option>
</SELECT>

이것을 위해 작동하는 Plunker를 만들었습니다. https://plnkr.co/edit/vR9aGoCwoOUL9tevIEen $('#console').append("<br/>"+$('#test_s :selected').text())


최신 브라우저에서는이를 위해 JQuery가 필요하지 않습니다. 대신 사용

document.querySelectorAll('option:checked')

또는 대신 DOM 요소를 지정하십시오. document


당신이 찾고있는 $select.html()

http://api.jquery.com/html/

참고 URL : https://stackoverflow.com/questions/2175737/how-to-get-label-of-select-option-with-jquery

반응형