반응형
jquery를 사용하여 텍스트별 드롭다운 값 설정
드롭다운은 다음과 같습니다.
<select id="HowYouKnow" >
<option value="1">FRIEND</option>
<option value="2">GOOGLE</option>
<option value="3">AGENT</option></select>
위 드롭다운에서 드롭다운의 텍스트를 알고 있습니다.jquery를 사용하여 텍스트로 document.ready에서 드롭다운 값을 설정하는 방법은 무엇입니까?
이 방법은 색인이 아닌 옵션 텍스트를 기반으로 작동하는 방법입니다.방금 테스트했습니다.
var theText = "GOOGLE";
$("#HowYouKnow option:contains(" + theText + ")").attr('selected', 'selected');
또는 유사한 값이 있는 경우(고맙습니다 샤나부스):
$("#HowYouKnow option").each(function() {
if($(this).text() == theText) {
$(this).attr('selected', 'selected');
}
});
정확한 매치 사용을 위해
$("#HowYouKnow option").filter(function(index) { return $(this).text() === "GOOGLE"; }).attr('selected', 'selected');
contains는 정확하지 않을 수 있는 마지막 일치 항목을 선택합니다.
$("#HowYouKnow option[value='" + theText + "']").attr('selected', 'selected'); // added single quotes
한번 해보세요.
$(element).find("option:contains(" + theText+ ")").attr('selected', 'selected');
var myText = 'GOOGLE';
$('#HowYouKnow option').map(function() {
if ($(this).text() == myText) return this;
}).attr('selected', 'selected');
구글, 구글다운, 구글업의 경우 코드 아래에서 시도할 수 있는 유사한 종류의 가치.
$("#HowYouKnow option:contains('GOOGLE')").each(function () {
if($(this).html()=='GOOGLE'){
$(this).attr('selected', 'selected');
}
});
이러한 방식으로 루프 반복 횟수를 줄일 수 있으며 모든 상황에서 작동할 것입니다.
아래 코드가 저에게 적합합니다 -:
jQuery('[id^=select_] > option').each(function(){
if (this.text.toLowerCase()=='text'){
jQuery('[id^=select_]').val(this.value);
}
});
jQuery('[id^=select_]') - 드롭다운 ID가 select_에서 시작하는 드롭다운을 선택할 수 있습니다.
위의 내용이 도움이 되기를 바랍니다!
치어스 S
간단한 예는 다음과 같습니다.
$("#country_id").change(function(){
if(this.value.toString() == ""){
return;
}
alert("You just changed country to: " + $("#country_id option:selected").text() + " which carried the value for country_id as: " + this.value.toString());
});
$("#HowYouKnow option:eq(XXX)").attr('selected', 'selected');
여기서 XXX는 원하는 인덱스입니다.
$("#HowYouKnow").val("GOOGLE");
이것은 크롬과 파이어폭스 둘다 작동합니다.
드롭다운 상자에 값을 설정합니다.
var given = $("#anotherbox").val();
$("#HowYouKnow").text(given).attr('value', given);
언급URL : https://stackoverflow.com/questions/1888931/set-dropdown-value-by-text-using-jquery
반응형
'programing' 카테고리의 다른 글
인덱스를 값으로 사용하여 목록을 사전으로 변환하는 방법은 무엇입니까? (0) | 2023.10.14 |
---|---|
ASP에 접속할 수 있습니까?인트라넷의 NET 개발 서버? (0) | 2023.10.14 |
WooCommerce를 통해 등록할 때 전체 이메일 주소를 사용자 이름으로 설정 (0) | 2023.10.14 |
sqlplus를 사용하여 Oracle DB에 연결 (0) | 2023.10.14 |
liquibase가 있는 Java에서 오래된 데이터베이스 스키마를 새 데이터베이스 스키마로 업데이트하는 방법 (0) | 2023.10.14 |