我正在为一些国家/地区使用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+"");
});
答案 0 :(得分: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链接,还包括更改国家/地区名称(以防您需要帮助:))