Getting the text from a drop-down box

后端 未结 12 2187
无人及你
无人及你 2020-11-30 02:23

This gets the value of whatever is selected in my dropdown menu.

document.getElementById(\'newSkill\').value

I cannot however find out what

相关标签:
12条回答
  • 2020-11-30 02:49

    Simply You can use Jquery instead of Javascript

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

    Try This.

    0 讨论(0)
  • 2020-11-30 02:52

    Does this get the correct answer?

    document.getElementById("newSkill").innerHTML
    
    0 讨论(0)
  • 2020-11-30 02:54
    document.getElementById('newSkill').options[document.getElementById('newSkill').selectedIndex].value 
    

    Should work

    0 讨论(0)
  • 2020-11-30 02:54

    Attaches a change event to the select that gets the text for each selected option and writes them in the div.

    You can use jQuery it very face and successful and easy to use

    <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();
    
    0 讨论(0)
  • 2020-11-30 02:56

    This works i tried it my self i thought i post it here in case someone need it...

    document.getElementById("newSkill").options[document.getElementById('newSkill').selectedIndex].text;
    
    0 讨论(0)
  • 2020-11-30 03:01
    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 Snippet

     <asp:DropDownList ID="ddl" runat="server" CssClass="ComboXXX" 
      onchange="getValue(this)">
    </asp:DropDownList>
    
    0 讨论(0)
提交回复
热议问题