jQuery 功能适用于一页上有一个 class 的所有下拉菜单

jQuery function for all dropdowns with one class on one page

我有问题。我需要下面的函数来分别处理具有相同 class 的 div。现在我有单独的 classes 来处理基本相同的事情 :(

我的代码:

<div class="selectDiv type">
    <span class="selectDefaultType"></span>
    <select name="txtType" class="selectBoxType">
        <option class="defaultTextType">IZVĒLIES VEIDU</option>
        <option value="1">Bārs</option>
        <option value="2">Nav bārs</option>
    </select>
</div>
<div class="selectDiv place">
    <span class="selectDefaultPlace"></span>
    <select name="txtPlace" class="selectBoxPlace">
        <option class="defaultTextPlace">IZVĒLIES PILSĒTU</option>
        <option value="1">Abkhazia</option>
        <option value="2">Afghanistan</option>
    </select>
</div>

<script>

var defaultTextPlace = $('.defaultTextPlace').text();
$('.selectDefaultPlace').text(defaultTextPlace);
$('.selectBoxPlace').on('change', function () {
    var defaultTextPlaceUpdated = $('.selectBoxPlace').find(":selected").text();
    $('.selectDefaultPlace').text(defaultTextPlaceUpdated);
});

var defaultTextType = $('.defaultTextType').text();
$('.selectDefaultType').text(defaultTextType);
$('.selectBoxType').on('change', function () {
    var defaultTextTypeUpdated = $('.selectBoxType').find(":selected").text();
    $('.selectDefaultType').text(defaultTextTypeUpdated);
});

</script>

<style>
.selectDiv {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    width: 100%;
    height: 50px;
    line-height: 50px;
    overflow: hidden;
    background: url('narvesen/img/openSelect.jpg') no-repeat right #f0f0f0;
    margin: 10px 0;
    padding: 0px;
    position: relative;
    z-index: 100;
    cursor: pointer;
    font-size: 14px;
    font-family:'Arimo', sans-serif;
}

.selectDefaultPlace, .selectDefaultType {
    text-align: center;
    display: inline-block;
    width: 100%;
    margin-left: -25px;
    font-size: 14px;
    font-family:'Arimo', sans-serif;
    text-transform: uppercase;
}

.selectDiv select {
    margin: 0px;
    padding: 0px;
    opacity: 0;
    filter: alpha(opacity=0);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: block;
    z-index: 101;
    cursor: pointer;
}

.selectDiv select:focus {
    outline: none;
}

</style>

fiddle!!!

我还在学习jQuery所以非常感谢任何帮助!

虽然您的代码没问题,但您可以使用 $.each:

稍微压缩一下
// look at all .selectDiv that have a select element and iterate over them separately...
$('.selectDiv select').each(function () {
    var select = $(this), // $(this) is the select in the current iteration
        defaultText = select.children().filter('[class]').filter(':first').text(), // get the first option, filter by class attribute
        label = select.siblings('span').filter(':first'); // a nicer way would be to give each span the same class like 'selectDefault' without Type or Place

    label.text(defaultText); // initially set the default text

    select.on('change', function () {
        var updated = select.val();
        label.text(updated);
    });
});

更好的方法
defaultText = select.children().filter('[class]').filter(':first').text()

将给每个 option 与默认文本函数相同 class:

 <option class="defaultTextType defaultText">IZVĒLIES VEIDU</option>
 ...
 <option class="defaultTextPlace defaultText">IZVĒLIES PILSĒTU</option>

然后输入

defaultText = select.children('.defaultText').text(),

更简单的方法:

Fiddle

HTML:

<div class="selectDiv type">
    <span class="selectDefault"></span>
    <select class="selectBox">
        <option value="0">IZVĒLIES VEIDU</option>
        <option value="1">Bārs</option>
        <option value="2">Nav bārs</option>
    </select>
</div>
<div class="selectDiv place">
    <span class="selectDefault"></span>
    <select class="selectBox">
        <option value="0">IZVĒLIES PILSĒTU</option>
        <option value="1">Abkhazia</option>
        <option value="2">Afghanistan</option>
    </select>
</div>

JS:

$('.selectBox').each(function(e){
    UpdateText($(this));
});

$('.selectBox').on('change', function () {
    UpdateText($(this));
});

function UpdateText(obj){
    var defaultTextUpdated = $(obj).find(":selected").text();  
    $(obj).parent().find('.selectDefault').text(defaultTextUpdated);

}

CSS更新:

.selectDefault {
    text-align: center;
    display: inline-block;
    width: 100%;
    margin-left: -25px;
    font-size: 14px;
    font-family:'Arimo', sans-serif;
    text-transform: uppercase;
}