我怎么可以选择的文本(不是所选择的数值)从下列在jQuery?

有帮助吗?

解决方案

$("#yourdropdownid option:selected").text();

其他提示

试试这个:

$("#myselect :selected").text();

对于ASP.NET下拉列表,您可以使用以下选择器:

$("[id*='MyDropDownId'] :selected")

这里发布的答案,例如,

$('#yourdropdownid option:selected').text();

对我不起作用,但确实如此:

$('#yourdropdownid').find('option:selected').text();

它可能是jQuery的旧版本。

如果您已经在变量中提供了下拉列表,那么这对我有用:

$("option:selected", myVar).text()

这个问题的其他答案对我有帮助,但最终jQuery论坛帖子 $(this + <!> quot; option:selected <!> quot;)。attr(<!> quot; rel <!> quot; )选择的选项在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()将给出所选的索引值。

这对我有用:

$('#yourdropdownid').find('option:selected').text();

jQuery 版本:1.9.1

对于所选项目的文本,请使用:

$('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 <!>”;和<!> quot; selText <!> quot;。您将获得所选的下拉值和文本

对于那些使用 SharePoint 列表并且不想使用long生成的ID的用户,这将有效:

var e = $('select[title="IntenalFieldName"] option:selected').text();
 $("#selectID option:selected").text();

而不是#selectID您可以使用任何jQuery选择器,例如.selectClass使用类。

如文档此处所述。

:所选选择器适用于<!> lt; option <!> gt;元素。它不适用于复选框或无线电输入;使用:checked为他们。

.text()根据此处文档。

获取匹配元素集合中每个元素的组合文本内容,包括它们的后代。

因此,您可以使用.text()方法从任何HTML元素中获取文本。

请参阅文档以获得更深入的解释。

$("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();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top