【问题标题】:how to add and remove classes depending on option selected in a select form如何根据在选择表单中选择的选项添加和删除类
【发布时间】:2012-01-13 10:34:22
【问题描述】:

我正在使用 jquery 移动自定义选择菜单来选择国家/地区。我想将按钮图标设置为国旗。

我有这个:

<div class="ui-select">
   // select button
   <a href="#" role="button" id="brandsByCountry-button" class="ui-btn ui-btn-up-c ui-btn-icon-right ui-btn-corner-all ui-shadow">
      <span class="ui-btn-inner ui-btn-corner-all">
         <span class="ui-btn-text">Germany</span>
         // country flag icon - insert country class here
         <span class="ui-icon ui-icon-lang ui-icon-shadow"></span>
      </span>
   </a>
   // selectmenu
   <select data-iconpos="right" class="ui-fake-icon" data-icon="lang" data-native-menu="false" id="brandsByCountry" name="brandsByCountry">
        <option data-placeholder="true" value="default">Country</option>
        <option value="be">Belgium</option>
        <option value="ch">Switzerland</option>
        <option value="de" selected="selected">Germany</option>
    </select>
 </div>

我可以通过将国家类从 val() 添加到 span.ui-icon 来设置标志。

问题:
当用户更改选择时,我需要添加一个新类并删除旧的国家/地区类。我不知道如何在不删除所有其他必要类的情况下删除该类。此外,我很难使用 val() 添加相应的类。

我有这个:

    $('#brands').bind( "pagebeforeshow", function( event, data ) {
   var initBrand = $('#brandsByCountry').val(),
       closestBtn = $('#brandsByCountry').prev('.ui-btn');

       closestBtn.addClass( initBrand ); // doesn't work
       closestBtn.addClass( "test" ); // works

       $('#brandsByCountry').live('change', function() {
            var str = $(this).children('option[selected]').val();
            console.log(str);  // ok, gets new class

            closestBtn.addClass( str ).removeClass(':not(".ui-icon, .ui-icon-lang, .ui-icon-shadow")');
            })
        })

感谢您的意见!

解决方案
这是我基于天际回答的最终版本。我添加了对多项选择的检查以及要显示的选择的默认值。

$('#brands').one( "pagebeforeshow", function( event, data ) {
     // initial class name
     $('#brandsByCountry').data('oldVal', 'global' );

     // designated element
     var closestBtn = $('#brandsByCountry').prev('a'), 
         orginalClass = $('#brandsByCountry').data('oldVal');

     // add inital class/icon
     closestBtn.addClass( orginalClass+"");

     $('#brandsByCountry').change(function() {

         var $this = $(this),
         // if more than one option is selected go back to global icon
         newClass = $this.val().length >= 2 ? "global" : $this.val(),
         oldClass = $this.data('oldVal');

         $this.data('oldVal', newClass);
         closestBtn.removeClass(oldClass+"").addClass(newClass+""); 
         });

【问题讨论】:

    标签: jquery forms select jquery-mobile toggleclass


    【解决方案1】:

    这里有一个完整的解决方案JSFiddle


    编辑我正在编辑它以包含所有代码(根据 JSFiddle 启动而减少)

    HTML:

    <div class="ui-select"> 
       <a href="#" role="button" id="brandsByCountry-button" class="ui-btn ui-btn-up-c ui-btn-icon-right ui-btn-corner-all ui-shadow"> 
          <span class="ui-btn-inner ui-btn-corner-all"> 
             <span class="ui-btn-text">Germany</span> 
             <span class="ui-icon ui-icon-lang ui-icon-shadow de">test</span> 
          </span> 
       </a> 
       <select data-iconpos="right" class="ui-fake-icon" data-icon="lang" data-native-menu="false" id="brandsByCountry" name="brandsByCountry"> 
            <option data-placeholder="true" value="default">Country</option> 
            <option value="be">Belgium</option> 
            <option value="ch">Switzerland</option> 
            <option value="de" selected="selected">Germany</option> 
        </select> 
    </div>
    

    CSS:

    .ui-icon.be { background-color: Red;}
    .ui-icon.ch { background-color: Green;}
    .ui-icon.de { background-color: Blue;}
    

    JS:

    $('#brandsByCountry').data('oldVal', $('#brandsByCountry').val());
    $('#brandsByCountry').change(function() {
        var $this = $(this);
        var newClass = $this.val();
        var oldClass = $this.data('oldVal');
        $this.data('oldVal', newClass);
    
        $('.ui-icon').removeClass(oldClass).addClass(newClass);
    });
    

    编辑我更新了JSFiddle链接(因为它不正确)


    编辑我再次更新了JSFiddle 链接,还包括更改国家/地区名称(以防万一您需要帮助:))

    【讨论】:

    • 你知道有什么奇怪的:你需要 removeClass(oldClass+"").addClass(newClass+""),否则什么都不会发生...我想知道为什么会触发更改但没有添加或删除任何类。添加了另一个“测试”类,然后它起作用了。一定是JQM的东西。所以它现在有效,我是一个快乐的露营者。非常感谢!
    • @frequent -- 这很奇怪,但感谢分享(记下:p)。
    • 我做了一些小修改(多选)并贴在上面。再次感谢!
    猜你喜欢
    • 2013-09-09
    • 2016-08-25
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多