例:
<select>
<option value='1'>hello me<option>
<option value='2'>hello world</option>
</select>
選択ボックスから値ではなくテキストにアクセスするにはどうすればよいですか
そのため、 1 & 2の代わりにHELLO MEまたはHELLO WORLDを表示できます。
フォーム セレクターtextと一緒に使用します。:selected
$('select option:selected').text()
すべてのオプションのテキストと値をループします。
$('#mySelect option').each(function () {
console.log(this.text + ' ' + this.value);
});
すべてのオプション テキストを配列に取得します。
var textArr = $('#mySelect option').map(function (i,n) {
return $(n).text();
}).get();
console.log(textArr.join(','));
選択したオプションのテキストにアクセスしたい場合は、次を使用できます。
$("#list option:selected").text();
または、次の方法でオプションに直接アクセスすることもできます。
$("#list option[value=2]").text();
#list特定の選択の ID です。<select id="list">他の選択タグとの競合を避けるために、id (例: ) を割り当てる必要があります。
var text = $(option[value='1']).text();