【问题标题】:jQuery take class from select optionjQuery从选择选项中取类
【发布时间】:2012-12-10 13:10:15
【问题描述】:

是否可以从 html select 中的选定选项中获取类名? 这是代码:

<select id="typ" name="typ">
    <option value="not">--- Zvoľte prosím typ zmluvy ---</option>
    <option value="12" class="DSL" >DSL</option>
    <option value="13" class="H" >H (Hlasové služby)</option>
</select>

如果用户选择 H (Hlasové služby) 我想在这个选项的 span class typ class 中写,在这种情况下它是 H 。

【问题讨论】:

    标签: jquery forms class select


    【解决方案1】:

    jquery 的解决方案非常简单:

    var SelectedOptionClass = $("#typ option:selected").attr("class");
    

    【讨论】:

      【解决方案2】:

      检查一下

      $("#typ").bind("change",function()
                         {
                             alert($(this).find("option:selected").attr("class"));
                         });
      

      http://jsfiddle.net/MzcjB/11/

      【讨论】:

        【解决方案3】:

        如果我没听错的话,您想将该类添加到已经具有类 typ&lt;span&gt;

        $('#typ').change( function() {
            $('span.typ').attr('class', 'tip').addClass('typ').addClass( $(this).find('option:selected').attr('class') );
        });​
        

        演示:http://jsfiddle.net/ELgSj/

        更新:

        我添加了attr('class', 'typ')。因此,当您重新更改select-tag 时,除typ 类之外的所有类都从&lt;span&gt; 中删除,仅添加选定的类。

        【讨论】:

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