반응형
드롭 다운 상자에서 텍스트 가져 오기
이것은 내 드롭 다운 메뉴에서 선택한 값을 가져옵니다.
document.getElementById('newSkill').value
그러나 현재 드롭 다운 메뉴에 표시된 텍스트에 대해 어떤 속성을 따라야할지 알 수 없습니다. "텍스트"를 시도한 다음 W3Schools 를 살펴 보았지만 답이 없었습니다. 여기있는 사람이 있습니까?
확실하지 않은 경우 드롭 다운 상자에 대한 HTML이 있습니다.
<select name="newSkill" id="newSkill">
<option value="1">A skill</option>
<option value="2">Another skill</option>
<option value="3">Yet another skill</option>
</select>
예제 HTML 코드를 기반으로 현재 선택된 옵션의 표시된 텍스트를 가져 오는 한 가지 방법은 다음과 같습니다.
var skillsSelect = document.getElementById("newSkill");
var selectedText = skillsSelect.options[skillsSelect.selectedIndex].text;
간단히 Javascript 대신 Jquery를 사용할 수 있습니다.
$("#yourdropdownid option:selected").text();
이 시도.
선택한 값의 텍스트 값을 반환해야합니다.
var vSkill = document.getElementById('newSkill');
var vSkillText = vSkill.options[vSkill.selectedIndex].innerHTML;
alert(vSkillText);
Props : @Tanerax는 질문을 읽고 다른 사람들이 알아 내기 전에 질문 한 내용을 알고 답변했습니다.
편집 : DownModed, 실제로 질문을 완전히 읽고 대답했기 때문에 슬픈 세상입니다.
document.getElementById('newSkill').options[document.getElementById('newSkill').selectedIndex].value
작동해야
이 작품은 내가 시도해 본 사람이 필요할 때를 대비하여 여기에 게시한다고 생각했습니다 ...
document.getElementById("newSkill").options[document.getElementById('newSkill').selectedIndex].text;
선택한 각 옵션에 대한 텍스트를 가져와 div에 기록하는 변경 이벤트를 선택 항목에 연결합니다.
jQuery는 매우 얼굴이 좋고 성공적이고 사용하기 쉽습니다.
<select name="sweets" multiple="multiple">
<option>Chocolate</option>
<option>Candy</option>
<option>Taffy</option>
<option selected="selected">Caramel</option>
<option>Fudge</option>
<option>Cookie</option>
</select>
<div></div>
$("select").change(function () {
var str = "";
$("select option:selected").each(function() {
str += $( this ).text() + " ";
});
$( "div" ).text( str );
}).change();
function getValue(obj)
{
// it will return the selected text
// obj variable will contain the object of check box
var text = obj.options[obj.selectedIndex].innerHTML ;
}
HTML 스 니펫
<asp:DropDownList ID="ddl" runat="server" CssClass="ComboXXX"
onchange="getValue(this)">
</asp:DropDownList>
이것이 정답을 얻었습니까?
document.getElementById("newSkill").innerHTML
var ele = document.getElementById('newSkill')
ele.onchange = function(){
var length = ele.children.length
for(var i=0; i<length;i++){
if(ele.children[i].selected){alert(ele.children[i].text)};
}
}
var selectoption = document.getElementById("dropdown");
var optionText = selectoption.options[selectoption.selectedIndex].text;
가장 쉬운 방법이며 완벽하게 작동합니다.
var newSkill_Text = document.getElementById("newSkill")[document.getElementById("newSkill").selectedIndex];
쉽고 짧은 방법입니다.
document.getElementById('elementID').selectedOptions[0].innerHTML
참고 URL : https://stackoverflow.com/questions/5913/getting-the-text-from-a-drop-down-box
반응형
'program tip' 카테고리의 다른 글
하나의 솔루션에서 여러 프로젝트간에 스크립트를 어떻게 공유합니까? (0) | 2020.10.29 |
---|---|
.net MVC Html.CheckBoxFor의 적절한 사용 (0) | 2020.10.29 |
Unix (또는 Windows)에서 (이름없는) 파이프를 사용하여 한 프로세스의 표준 출력을 여러 프로세스로 어떻게 보낼 수 있습니까? (0) | 2020.10.29 |
Mac OSX 매버릭스에서 Maven을 찾을 수 없음 (0) | 2020.10.28 |
UIApplication.registerForRemoteNotifications ()는 메인 스레드에서만 호출되어야합니다. (0) | 2020.10.28 |