質問

これにより、ドロップダウン メニューで選択された値が取得されます。

document.getElementById('newSkill').value

ただし、現在ドロップダウン メニューに表示されているテキストのどのプロパティを追跡すればよいのかわかりません。「テキスト」を試してみました W3スクール でも答えはなかったのですが、ここにいる人は知っていますか?

よくわからない人のために、ドロップダウン ボックスの HTML を次に示します。

<select name="newSkill" id="newSkill">
    <option value="1">A skill</option>
    <option value="2">Another skill</option>
    <option value="3">Yet another skill</option>
</select>
役に立ちましたか?

解決

HTML コードの例に基づいて、現在選択されているオプションの表示テキストを取得する 1 つの方法を次に示します。

var skillsSelect = document.getElementById("newSkill");
var selectedText = skillsSelect.options[skillsSelect.selectedIndex].text;

他のヒント

これにより、選択された値のテキスト値が返されます。

var vSkill = document.getElementById('newSkill');

var vSkillText = vSkill.options[vSkill.selectedIndex].innerHTML;

alert(vSkillText);

小道具:@Tanerax、質問を読み、何が質問されているかを理解し、他の人が理解する前に答えてくれました。

編集:DownModed、なぜなら私は実際に質問をよく読み、それに答えたからです、悲しい世界です。

単純にJavaScriptの代わりにJqueryを使用できます

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

これを試して。

document.getElementById('newSkill').options[document.getElementById('newSkill').selectedIndex].value 

うまくいくはずです

これはうまくいきます、自分で試してみました、誰かが必要とする場合に備えてここに投稿しようと思いました...

document.getElementById("newSkill").options[document.getElementById('newSkill').selectedIndex].text;

選択した各オプションのテキストを取得して div に書き込む変更イベントを select にアタッチします。

jQuery を非常に簡単に使用でき、成功し、使いやすくなります。

<select name="sweets" multiple="multiple">
  <option>Chocolate</option>
  <option>Candy</option>
  <option>Taffy</option>
  <option selected="selected">Caramel</option>
  <option>Fudge</option>
  <option>Cookie</option>
</select>
<div></div>


$("select").change(function () {
  var str = "";

  $("select option:selected").each(function() {
    str += $( this ).text() + " ";
  });

  $( "div" ).text( str );
}).change();

これで正しい答えが得られますか?

document.getElementById("newSkill").innerHTML
function getValue(obj)
{  
   // it will return the selected text
   // obj variable will contain the object of check box
   var text = obj.options[obj.selectedIndex].innerHTML ; 

}

HTML スニペット

 <asp:DropDownList ID="ddl" runat="server" CssClass="ComboXXX" 
  onchange="getValue(this)">
</asp:DropDownList>
    var ele = document.getElementById('newSkill')
    ele.onchange = function(){
            var length = ele.children.length
            for(var i=0; i<length;i++){
                if(ele.children[i].selected){alert(ele.children[i].text)};              
            }
    }   
var selectoption = document.getElementById("dropdown");
var optionText = selectoption.options[selectoption.selectedIndex].text;

以下を試してください。これが最も簡単な方法であり、完全に機能します

var newSkill_Text = document.getElementById("newSkill")[document.getElementById("newSkill").selectedIndex];

簡単で短い方法は次のとおりです

document.getElementById('elementID').selectedOptions[0].innerHTML
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top