当用户更改选项时,嘿我需要的是:
例如,如果用户选择第二个选项,则必须返回2 ..等等。
我的代码如下:
$('.selectContainer').on('change', function () {
var index = $(this).find('.selectCar').data('index');
alert(index);
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select class="selectContainer">
<option class="selectCar" data-index="1">
one
</option>
<option class="selectCar" data-index="2">
two
</option>
<option class="selectCar" data-index="3">
three
</option>
<option class="selectCar" data-index="4">
four
</option>
</select>
&#13;
答案 0 :(得分:2)
使用$(this).find('option:selected').data('index');
$('.selectContainer').on('change', function() {
var index = $(this).find('option:selected').data('index');
alert(index);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select class="selectContainer">
<option class="selectCar" data-index="1">
one
</option>
<option class="selectCar" data-index="2">
two
</option>
<option class="selectCar" data-index="3">
three
</option>
<option class="selectCar" data-index="4">
four
</option>
</select>
答案 1 :(得分:1)
$('.selectContainer').on('change', function () {
var index = $(this).find('.selectCar:selected').data('index');
alert(index);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select class="selectContainer">
<option class="selectCar" data-index="1">
one
</option>
<option class="selectCar" data-index="2">
two
</option>
<option class="selectCar" data-index="3">
three
</option>
<option class="selectCar" data-index="4">
four
</option>
</select>
答案 2 :(得分:0)
更新你的jQuery代码,如as -
$('.selectContainer').change(function () {
var index = $(this).find('.selectCar:selected').data('index');
console.log(index);
});