-1

我的页面上有多个动态设置的选择框(比如超过 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>
4

1 回答 1

7
$('.selection').change(function() {
    console.log(this.value);
    //or
    console.log($(this).val());
});
于 2012-09-23T23:52:53.900 回答