jQuery를 사용하여 드롭 다운 목록 (선택 상자)에서 선택한 텍스트 가져 오기
-
10-07-2019 - |
문제
jQuery의 드롭 다운 목록에서 선택한 텍스트 (선택된 값이 아님)를 어떻게 얻을 수 있습니까?
해결책
$("#yourdropdownid option:selected").text();
다른 팁
이 시도:
$("#myselect :selected").text();
ASP.NET 드롭 다운의 경우 다음 선택기를 사용할 수 있습니다.
$("[id*='MyDropDownId'] :selected")
예를 들어 여기에 게시 된 답변
$('#yourdropdownid option:selected').text();
나를 위해 효과가 없었지만 이것은 다음과 같습니다.
$('#yourdropdownid').find('option:selected').text();
아마도 이전 버전의 jQuery 일 것입니다.
이미 변수로 DropdownList를 사용할 수있는 경우 이것이 나에게 효과적입니다.
$("option:selected", myVar).text()
이 질문에 대한 다른 답변은 저를 도왔지만 궁극적으로 jQuery 포럼 스레드 $ (이 + "옵션 : 선택한"). attr ( "Rel") 옵션이 선택되지 않습니다. IE에서는 작동하지 않습니다. 가장 도움이되었습니다.
업데이트 : 위의 링크를 수정했습니다
$("option:selected", $("#TipoRecorde")).text()
이것은 나를 위해 작동합니다
$("#dropdownid").change(function() {
alert($(this).find("option:selected").text());
});
요소가 동적으로 생성 된 경우
$(document).on("change", "#dropdownid", function() {
alert($(this).find("option:selected").text());
});
$("#DropDownID").val()
선택한 인덱스 값을 제공합니다.
선택한 항목의 텍스트의 경우 다음을 사용하십시오.
$('select[name="thegivenname"] option:selected').text();
선택한 항목의 값은 다음을 사용하십시오.
$('select[name="thegivenname"] option:selected').val();
다양한 방법
1. $("#myselect option:selected").text();
2. $("#myselect :selected").text();
3. $("#myselect").children(":selected").text();
4. $("#myselect").find(":selected").text();
$("#dropdownID").change(function(){
alert($('option:selected', $(this)).text());
});
var someName = "Test";
$("#<%= ddltest.ClientID %>").each(function () {
$('option', this).each(function () {
if ($(this).text().toLowerCase() == someName) {
$(this).attr('selected', 'selected')
};
});
});
그것은 당신이 올바른 방향을 얻는 데 도움이 될 것입니다. 추가 도움이 필요하면 위의 코드가 완전히 테스트됩니다.
이것을 사용하십시오
var listbox = document.getElementById("yourdropdownid");
var selIndex = listbox.selectedIndex;
var selValue = listbox.options[selIndex].value;
var selText = listbox.options[selIndex].text;
그런 다음 "selvalue"및 "seltext"를 알려주십시오. 선택한 드롭 다운 값과 텍스트를 얻습니다
사용하는 사람들을 위해 공유 지점 목록을 작성하고 오래 생성 된 ID를 사용하고 싶지 않으면 다음이 작동합니다.
var e = $('select[title="IntenalFieldName"] option:selected').text();
$("#selectID option:selected").text();
대신에 #selectID
jQuery 선택기와 같은 모든 jQuery 선택기를 사용할 수 있습니다 .selectClass
수업 사용.
문서에서 언급했듯이 여기.
: 선택한 선택기가 작동합니다u003Coption> 집단. 확인란이나 무선 입력에는 작동하지 않습니다. 사용 :checked
그들을 위해.
.텍스트() 문서에 따라 여기.
자손을 포함하여 일치하는 요소 세트에서 각 요소의 결합 된 텍스트 내용을 가져옵니다.
따라서 HTML 요소에서 .text()
방법.
더 깊은 설명은 문서를 참조하십시오.
$("select[id=yourDropdownid] option:selected").text()
이것은 잘 작동합니다
$('#id').find('option:selected').text();
선택된 가치 사용을 위해
$('#dropDownId').val();
선택한 항목 텍스트를 얻으려면이 줄을 사용하십시오.
$("#dropDownId option:selected").text();
드롭 다운/jQuery에서 텍스트 및 선택한 값을 선택하십시오.
$("#yourdropdownid").change(function() {
console.log($("option:selected", this).text()); //text
console.log($(this).val()); //value
})
사용:
('#yourdropdownid').find(':selected').text();
var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;
다음은 저를 위해 일했습니다.
$.trim($('#dropdownId option:selected').html())
형제 사건에서
<a class="uibutton confirm addClient" href="javascript:void(0);">ADD Client</a>
<input type="text" placeholder="Enter client name" style="margin: 5px;float: right" class="clientsearch large" />
<select class="mychzn-select clientList">
<option value="">Select Client name....</option>
<option value="1">abc</option>
</select>
/*jQuery*/
$(this).siblings('select').children(':selected').text()
이것은 나를위한 일 :
$("#city :selected").text();
jQuery 1.10.2를 사용하고 있습니다
노력하다:
$var = jQuery("#dropdownid option:selected").val();
alert ($var);
또는 옵션의 텍스트를 얻으려면 사용하십시오. text()
:
$var = jQuery("#dropdownid option:selected").text();
alert ($var);
더 많은 정보:
$(function () {
alert('.val() = ' + $('#selectnumber').val() + ' AND html() = ' + $('#selectnumber option:selected').html() + ' AND .text() = ' + $('#selectnumber option:selected').text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="selectnumber">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
</select>
</div>
</form>
</body>
</html>
다음 코드를 사용해보십시오.
var text= $('#yourslectbox').find(":selected").text();
선택한 옵션의 텍스트를 반환합니다.
결과를 목록으로 원한다면 다음을 사용하십시오.
x=[];
$("#list_id").children(':selected').each(function(){x.push($(this).text());})
다중 선택의 경우 :
$("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
$("#dropdownid option:selected").text();
asp.net을 사용하고 쓰기
<Asp:dropdownlist id="ddl" runat="Server" />
그런 다음 사용해야합니다
$('#<%=ddl.Clientid%> option:selected').text();