programing

jQuery로 select 옵션의 레이블을 가져오는 방법은 무엇입니까?

javamemo 2023. 8. 8. 19:56
반응형

jQuery로 select 옵션의 레이블을 가져오는 방법은 무엇입니까?

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

값은 다음을 통해 검색할 수 있습니다.$select.val().

무엇이 말이야.label?

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

사용해 보십시오.

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

안녕하세요, 우선 선발자에게 아이디를 주세요.

<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[name=users] option:selected').text()

를 사용하여 셀렉터에 액세스할 때this키워드

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

사용해 보십시오.

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

두 스타일 모두에 대해 표시된 텍스트를 꺼냅니다.<option>요소:

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

둘 다 있는 경우label요소 내부의 속성과 텍스트를 사용합니다.label속성, 브라우저와 동일한 동작입니다.

후세를 위해, 이것은 jQuery 3.1.1에 따라 테스트되었습니다.

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

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

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

document.querySelectorAll('option:checked')

또는 다음 대신 DOM 요소를 지정합니다.document

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

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

찾으시는 제품$select.html()

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

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

반응형