【问题标题】:Get selected text of a combo box using jQuery, by name of the combo box?使用 jQuery 通过组合框的名称获取组合框的选定文本?
【发布时间】:2012-01-13 11:47:04
【问题描述】:

如何使用 jQuery 从组合框中获取选定的文本值。

我只有组合框的“名称”。

所以,我想要所选项目的文本,使用组合框的名称,而不是 ID。

我有,

var selected_fld = ( $(this).attr('name') );

我该如何继续?

【问题讨论】:

    标签: jquery combobox


    【解决方案1】:
    $('select[name=nameOfTheBox]').val();
    

    $('select[name=nameOfTheBox] option:selected').val();
    

    将为您提供所选选项的值

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

    会给你它的文字

    【讨论】:

      【解决方案2】:

      这可以通过以下简单的方式来获得实际的文本值...

      var value = $("[name='MyName'] option:selected").text();
      

      或者这个来获取选项'value'属性...

      var value = $("[name='MyName']").val();
      

      使用以下html,第一个会给你'MyText',第二个会给你'MyValue'

      <select name="MyName">
         <option value="MyValue" selected="selected">MyText</option>
      </select>
      

      Here is a working example

      【讨论】:

        【解决方案3】:

        尝试关注

        <select name="name1">
              <option value="val1">val1</option>
              <option value = "val2">val2</option>
        </select>
        
        var text = $("select[name='name1'] option:selected").text();
        

        【讨论】:

        • selected 后面的单引号必须是双引号
        【解决方案4】:
        <select id ="myCombo">
           <option value="Play selected="selected">Here</option>
           <option value="Once">Again</option>
        </select>
        
        $('#myCombo option:selected').text();
        

        这会让你回到“这里”

        $('myCombo option:selected').val();
        

        这将返回“播放”

        【讨论】:

          【解决方案5】:
              <html xmlns="http://www.w3.org/1999/xhtml">
          <head runat="server">
              <title></title>
              <script src="jquery-3.1.0.js"></script>
              <script>
                  $(function () {
                      $('#selectnumber').change(function(){
                          alert('.val() = ' + $('#selectnumber').val() + '  AND  html() = ' + $('#selectnumber option:selected').html() + '  AND .text() = ' + $('#selectnumber option:selected').text());
                      })
                  });
              </script>
          </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>
          

          谢谢... :)

          【讨论】:

            猜你喜欢
            • 2011-08-08
            • 1970-01-01
            • 2014-06-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多