首页 文章

使用jQuery从下拉列表(选择框)中获取所选文本

提问于
浏览
2065

如何从jQuery的下拉列表中获取所选文本(而不是选定的值)?

30 回答

  • 5

    试试这个:

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

    对于ASP.NET下拉列表,您可以使用以下选择器:

    $("[id*='MyDropDownId'] :selected")
    
  • 7

    $("#DropDownID").val() 将给出所选的索引值 .

  • 0

    只需尝试以下代码即可 .

    var text= $('#yourslectbox').find(":selected").text();
    

    它返回所选选项的文本 .

  • 1
    $("option:selected", $("#TipoRecorde")).text()
    
  • 190
    $("#dropdownID").change(function(){
      alert($('option:selected', $(this)).text());
    });
    
  • 53

    这对我有用:

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

    jQuery版本:1.9.1

  • 18
    var e = document.getElementById("dropDownId");
    var div = e.options[e.selectedIndex].text;
    
  • 16
    $("select[id=yourDropdownid] option:selected").text()
    

    这很好用

  • 9

    对于多选:

    $("#yourdropdownid :selected").map(function(i, v) { return $.trim($(v).text()); }
    
  • 61

    例如,这里发布的答案,

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

    不适合我,但这样做:

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

    它可能是jQuery的旧版本 .

  • 51
    $("#selectID option:selected").text();
    

    您可以使用任何jQuery选择器代替 #selectID ,例如 .selectClass 使用类 .

    如文档here中所述 .

    :selected选择器适用于<option>元素 . 它不适用于复选框或无线电输入;使用 :checked 为他们 .

    .text() 根据文档here .

    获取匹配元素集中每个元素的组合文本内容,包括它们的后代 .

    因此,您可以使用 .text() 方法从任何HTML元素中获取文本 .

    请参阅文档以获得更深入的解释 .

  • 5

    在兄弟情况下

    <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()
    
  • 14
    var someName = "Test";
    
    $("#<%= ddltest.ClientID %>").each(function () {
        $('option', this).each(function () {
            if ($(this).text().toLowerCase() == someName) {
                $(this).attr('selected', 'selected')
            };
        });
    });
    

    这将有助于您获得正确的方向 . 如果您需要进一步的帮助,请通过以上代码进行全面测试 .

  • 3

    对于所选项目的文本,请使用:

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

    对于所选项目的值,请使用:

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

    在下拉列表中选择文本和选定的值/在jQuery中选择更改事件

    $("#yourdropdownid").change(function() {
        console.log($("option:selected", this).text()); //text
        console.log($(this).val()); //value
    })
    
  • 243
    $(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>
    
  • 12

    如果您希望将结果作为列表,请使用:

    x=[];
    $("#list_id").children(':selected').each(function(){x.push($(this).text());})
    
  • 95
    $("#yourdropdownid option:selected").text();
    
  • 32

    尝试:

    $var = jQuery("#dropdownid option:selected").val();
       alert ($var);
    

    或者要获取选项的文本,请使用 text()

    $var = jQuery("#dropdownid option:selected").text();
       alert ($var);
    

    More Info:

  • 12
    $("#dropdownid option:selected").text();
    

    如果你使用asp.net并写

    <Asp:dropdownlist id="ddl" runat="Server" />
    

    那你应该用

    $('#<%=ddl.Clientid%> option:selected').text();
    
  • 28

    使用:

    ('#yourdropdownid').find(':selected').text();
    
  • 54

    请用这个

    var listbox = document.getElementById("yourdropdownid");
    var selIndex = listbox.selectedIndex;
    var selValue = listbox.options[selIndex].value;
    var selText = listbox.options[selIndex].text;
    

    然后请提醒“selValue”和“selText” . 您将获得所选的下拉值和文本

  • 18

    对于那些使用SharePoint列表并且不想使用long生成的id的人,这将起作用:

    var e = $('select[title="IntenalFieldName"] option:selected').text();
    
  • 6

    这项工作对我来说:

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

    我正在使用jQuery 1.10.2

  • 3436

    这对我有用

    $("#dropdownid").change(function() {
        alert($(this).find("option:selected").text());
    });
    

    如果元素是动态创建的

    $(document).on("change", "#dropdownid", function() {
        alert($(this).find("option:selected").text());
    });
    
  • 26

    以下对我有用:

    $.trim($('#dropdownId option:selected').html())
    
  • 0

    如果您已经在变量中提供了下拉列表,这对我有用:

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

    关于这个问题的其他答案对我有帮助,但最终jQuery论坛帖子$(this + "option:selected").attr("rel") option selected is not working in IE帮助最多 .

    更新:修复了上述链接

  • 41
    $('#id').find('option:selected').text();
    
  • 10

    用于获取选定的值

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

    并获取所选项目文本使用此行:

    $("#dropDownId option:selected").text();
    
  • 7

    各种方式

    1. $("#myselect option:selected").text();
    
    2. $("#myselect :selected").text();
    
    3. $("#myselect").children(":selected").text();
    
    4. $("#myselect").find(":selected").text();
    

相关问题