2435

jQueryのドロップダウンリストから選択したテキスト(選択した値ではない)を取得するにはどうすればよいですか?

4

35 に答える 35

3929
$("#yourdropdownid option:selected").text();
于 2009-10-29T12:05:38.997 に答える
278

これを試して:

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

ASP.NET ドロップダウンでは、次のセレクターを使用できます。

$("[id*='MyDropDownId'] :selected")
于 2009-10-29T12:04:28.220 に答える
229

ここに投稿された回答は、たとえば、

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

私にはうまくいきませんでしたが、これはうまくいきました:

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

jQuery の古いバージョンである可能性があります。

于 2012-03-19T11:41:05.053 に答える
113

変数で利用可能なドロップダウンリストが既にある場合、これが私にとってはうまくいきます:

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

この質問に関する他の回答は私を助けましたが、最終的に jQuery フォーラムのスレッド$(this + "option:selected").attr("rel") option selected is not working in IEが最も役に立ちました。

更新:上記のリンクを修正

于 2012-02-04T05:17:05.993 に答える
68
$("option:selected", $("#TipoRecorde")).text()
于 2011-08-16T17:56:44.587 に答える
55

これは私のために働く:

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

jQueryバージョン: 1.9.1

于 2013-09-25T07:36:47.823 に答える
54

$("#DropDownID").val() 選択したインデックス値を提供します。

于 2011-11-14T09:22:21.090 に答える
44

選択したアイテムのテキストには、次を使用します。

$('select[name="thegivenname"] option:selected').text();

選択したアイテムの値には、次を使用します。

$('select[name="thegivenname"] option:selected').val();
于 2013-01-24T23:22:46.073 に答える
36

これを使って

const select = document.getElementById("yourSelectId");

const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;   

次に、選択した値とテキストを and 内に取得しselectedValueますselectedText

于 2015-05-14T14:21:49.810 に答える
35

色々な方法

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

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

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

4. $("#myselect").find(":selected").text();
于 2014-08-03T19:39:08.500 に答える
30
var someName = "Test";

$("#<%= ddltest.ClientID %>").each(function () {
    $('option', this).each(function () {
        if ($(this).text().toLowerCase() == someName) {
            $(this).attr('selected', 'selected')
        };
    });
});

それはあなたが正しい方向性を得るのに役立ちます。上記のコードは完全にテストされており、さらにサポートが必要な場合はお知らせください。

于 2010-10-07T10:09:55.793 に答える
17
 $("#selectID option:selected").text();

代わりに、クラス#selectIDを使用するように、任意の jQuery セレクターを使用できます.selectClass

こちらのドキュメントに記載されているとおりです。

:selected セレクターは <option> 要素に対して機能します。チェックボックスやラジオ入力では機能しません。それらに使用:checkedします。

.text()ここのドキュメントに従って。

子孫を含む、一致した要素のセット内の各要素の結合されたテキスト コンテンツを取得します。

したがって、メソッドを使用して任意の HTML 要素からテキストを取得できます.text()

詳細な説明については、ドキュメントを参照してください。

于 2014-01-30T14:29:42.697 に答える
13
$('#id').find('option:selected').text();
于 2015-04-11T09:39:05.860 に答える
13

選択した値を取得するために使用します

$('#dropDownId').val();

選択したアイテムのテキストを取得するには、次の行を使用します。

$("#dropDownId option:selected").text();
于 2016-01-23T09:07:31.777 に答える
11
var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;
于 2017-01-04T12:53:48.533 に答える
9

このコードは私のために働いた。

$("#yourdropdownid").children("option").filter(":selected").text();
于 2020-02-13T16:17:09.003 に答える
8

以下は私のために働いた:

$.trim($('#dropdownId option:selected').html())
于 2015-09-17T05:13:23.083 に答える
7

兄弟の場合

<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()
于 2014-12-05T06:33:34.873 に答える
6

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

クリックして出力画面を表示

于 2016-08-09T17:01:49.927 に答える
2

複数選択の場合:

$("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
于 2018-03-15T22:21:12.293 に答える