我有问题。我需要下面的函数分别处理具有相同类的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>
我还在学习jQuery,所以非常感谢任何帮助!
答案 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)
更简单的方法:
<强> 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;
}