0

国を選択するために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")');
            })
        })

ご意見ありがとうございます。

ここでの解決策
は、Skyrimの回答に基づく私の最終バージョンです。複数選択のチェックと、表示する選択のデフォルト値を追加しました。

$('#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+""); 
         });
4

1 に答える 1

1

Here is a complete solution at JSFiddle


EDIT I'm editting this to include all the code here (to reduce depending on JSFiddle being up)

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);
});

EDIT I update the JSFiddle link (as it was incorrect)


EDIT I updated the JSFiddle link again to also include changing the country name (just in case you need help with that :) )

于 2012-01-13T10:58:34.170 に答える