Get selected text from a drop-down list (select bo

2018-12-31 02:32发布

How can I get the selected text (not the selected value) from a drop-down list in jQuery?

30条回答
长期被迫恋爱
2楼-- · 2018-12-31 03:11

Use:

('#yourdropdownid').find(':selected').text();
查看更多
只若初见
3楼-- · 2018-12-31 03:12

$(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>

Click to see OutPut Screen

查看更多
君临天下
4楼-- · 2018-12-31 03:13

$("#DropDownID").val() will give the selected index value.

查看更多
人气声优
5楼-- · 2018-12-31 03:14

The answers posted here, for example,

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

didn't work for me, but this did:

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

It is possibly an older version of jQuery.

查看更多
裙下三千臣
6楼-- · 2018-12-31 03:15
$("option:selected", $("#TipoRecorde")).text()
查看更多
冷夜・残月
7楼-- · 2018-12-31 03:15
 $("#selectID option:selected").text();

Instead of #selectID you can use any jQuery selector, like .selectClass using class.

As mentioned in the documentation here.

The :selected selector works for <option> elements. It does not work for checkboxes or radio inputs; use :checked for them.

.text() As per the documentation here.

Get the combined text contents of each element in the set of matched elements, including their descendants.

So you can take text from any HTML element using the .text() method.

Refer the documentation for a deeper explanation.

查看更多
登录 后发表回答