一个页面上有一个类的所有下拉列表的jQuery函数

时间:2015-10-08 09:36:44

标签: jquery this dropdown

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

我的代码:

<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,所以非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

虽然你的代码没问题,你可以使用$.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提供相同类的默认文本函数:

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

然后输入

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

答案 1 :(得分:1)

更简单的方法:

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