jQueryのドロップダウンリストから選択したテキスト(選択した値ではない)を取得するにはどうすればよいですか?
jQueryのドロップダウンリストから選択したテキスト(選択した値ではない)を取得するにはどうすればよいですか?
回答:
$("#yourdropdownid option:selected").text();
              $("#yourdropdownid").children("option").filter(":selected").text()is()はオブジェクトがセレクターと一致するかどうかのブール値を返すので、これは当然だと思います。
                    is("selected").text()は、aを返しますTypeError: Object false has no method 'text'
                    $('select').children(':selected')最速の方法です:jsperf.com/get-selected-option-text
                    これを試して:
$("#myselect :selected").text();
ASP.NETドロップダウンの場合、次のセレクターを使用できます。
$("[id*='MyDropDownId'] :selected")
              ("#ct0001yourdropdownid) 
                    IDコントロールにを置くかどうかなどに基づいており、そうでない場合は、ツリーの現在のレベルで発生する場所のインデックスに基づくなど)。 )
                    たとえば、ここに投稿された回答は、
$('#yourdropdownid option:selected').text();
私にとってはうまくいきませんでしたが、これはうまくいきました:
$('#yourdropdownid').find('option:selected').text();
おそらく古いバージョンのjQueryです。
変数にドロップダウンリストが既にある場合、これは私にとってはうまくいきます:
$("option:selected", myVar).text()
この質問に対する他の回答は私を助けましたが、選択されたjQueryフォーラムのスレッド$(this + "option:selected")。attr( "rel")オプションは、IEで最も効果的に機能しません。
更新:上記のリンクを修正
changeイベントでは$(this).find('option:selected').text()テキストを取得するためにこれを使用できるので、これは私にとってはうまくいきました。
                    $(this).children(':selected').text()も機能します。@ Timo002
                    $("#dropdownID").change(function(){
  alert($('option:selected', $(this)).text());
});
              $(this)  私のajaxコールの後に期待していたものです
                    これを使って
const select = document.getElementById("yourSelectId");
const selectedIndex = select.selectedIndex;
const selectedValue = select.value;
const selectedText = select.options[selectedIndex].text;   
次に、選択した値とテキストをselectedValueと内に取得しますselectedText。
使用している人のためのSharePointリストをと長い生成されたIDを使用したくない、これは動作します:
var e = $('select[title="IntenalFieldName"] option:selected').text();
               $("#selectID option:selected").text();
代わりに、クラスの使用#selectIDなど、任意のjQueryセレクターを使用できます.selectClass。
:selectedセレクターは<option>要素に対して機能します。チェックボックスやラジオ入力には機能しません。:checkedそれらのために使用します。
.text()のドキュメントを1としてここに。
子孫を含む、一致する要素のセット内の各要素の結合されたテキストコンテンツを取得します。
したがって、.text()メソッドを使用して、任意のHTML要素からテキストを取得できます。
詳細な説明については、ドキュメントを参照してください。
$("select[id=yourDropdownid] option:selected").text()
これはうまくいきます
jQueryのドロップダウンでテキストと選択した値を選択/変更イベントを選択
$("#yourdropdownid").change(function() {
    console.log($("option:selected", this).text()); //text
    console.log($(this).val()); //value
})
              var e = document.getElementById("dropDownId");
var div = e.options[e.selectedIndex].text;
              var div = e.options[e.selectedIndex].text;てみましたが、最初のオプション項目しか表示されません。各アイテムを表示するにはどうすればよいですか?
                    私にとってこの作品:
$("#city :selected").text();
私はjQuery 1.10.2を使用しています
兄弟の場合
<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()
              $(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 = jQuery("#dropdownid option:selected").val();
   alert ($var);
または、オプションのテキストを取得するには、次を使用しますtext()。
$var = jQuery("#dropdownid option:selected").text();
   alert ($var);
より詳しい情報: