我的页面上有多个动态设置的选择框(超过100个)。我试图在更改时单独获取每个框的值,但是当发生更改时,它会迭代每个选择框,因为它们都具有相同的类。如何单独获取每个选择框的值然后执行某些操作...
示例
<select id="(dynamically set, may be different every time)" class="selection">
<option value=""></option>
<option value="dynamically set">1</option><option value="dynamically set">2</option>
<option value="dynamically set">3</option>
</select>
<select id="(dynamically set, may be different every time)" class="selection">
<option value=""></option>
<option value="dynamically set">1</option>
<option value="dynamically set">2</option>
<option value="dynamically set">3</option></select>
<select id="(dynamically set, may be different every time)" class="selection">
<option value=""></option>
<option value="dynamically set">1</option>
<option value="dynamically set">2</option>
<option value="dynamically set">3</option></select>
etc...
JS
<script type="text/javascript">
//Psuedo code
$('.selection').change(function(){alert('test');});
</script>
答案 0 :(得分:7)
$('.selection').change(function() {
console.log(this.value);
//or
console.log($(this).val());
});