Javascript change-获取事件源

Javascript change-获取事件源,javascript,jquery,Javascript,Jquery,我在HTML表单上有许多元素,类似于以下内容: <div class="area"> First Area <select class="area_select" name="area_25" id="form_area_25"> <option value="0" selected="selected">No</option> <option value="1" >Don't Mind&

我在HTML表单上有许多元素,类似于以下内容:

<div class="area">
    First Area
    <select class="area_select" name="area_25" id="form_area_25">
        <option value="0" selected="selected">No</option>
        <option value="1" >Don't Mind</option>
        <option value="2" >Yes</option>
    </select>
</div>

<div class="area">
    Second Area
    <select class="area_select" name="area_13" id="form_area_13">
        <option value="0" selected="selected">No</option>
        <option value="1" >Don't Mind</option>
        <option value="2" >Yes</option>
    </select>
</div>
    .... and many more

有没有办法让事件处理程序确切地知道哪个select是事件的源?

您可以对源对象使用
this
,对源对象使用
this.id

$(".area_select").change(function(event) {
  alert(this.id);
});

您可以对源对象使用
this
,对源对象使用
this.id

$(".area_select").change(function(event) {
  alert(this.id);
});

偶数中的此
指触发事件的控件:

$('.area_select').change(function(){
  // this = the control
});
这就是你想要的吗?或者您可以在回调中接受
e
,并查看
e.target

$('.area_select').change(function(e){
  // e.target = the control (unless bubbling occurred)
});
文件

如果您想获得select的ID(只是号码),可以使用:

$(".area_select").change(function() {
    var selectid = this.id.match(/(\d+)$/)[1];
});

偶数中的此
指触发事件的控件:

$('.area_select').change(function(){
  // this = the control
});
这就是你想要的吗?或者您可以在回调中接受
e
,并查看
e.target

$('.area_select').change(function(e){
  // e.target = the control (unless bubbling occurred)
});
文件

如果您想获得select的ID(只是号码),可以使用:

$(".area_select").change(function() {
    var selectid = this.id.match(/(\d+)$/)[1];
});
您可以使用
$(this)
获取触发事件的控件

$(".area_select").change(function() {
   alert($(this).attr("id") + " changed");
});
您可以使用
$(this)
获取触发事件的控件

$(".area_select").change(function() {
   alert($(this).attr("id") + " changed");
});

这是源代码。实际上,绑定到事件的函数是在事件发生的项的上下文中调用的。所以很简单:

$(".area_select").change(function() {
  alert('select ' + $(this).attr("id") + " changed");
});

这是源代码。实际上,绑定到事件的函数是在事件发生的项的上下文中调用的。所以很简单:

$(".area_select").change(function() {
  alert('select ' + $(this).attr("id") + " changed");
});

我想你需要这个

$(".area_select").change(function() {
      alert('select changed: '+$(this).val()+' , but no idea which one: '
      +$(this).attr('id').replace('form_area_',''));
});

我想你需要这个

$(".area_select").change(function() {
      alert('select changed: '+$(this).val()+' , but no idea which one: '
      +$(this).attr('id').replace('form_area_',''));
});
您应该使用,特别是如果您要访问本机。更好的方法是完全排除jQuery,而只使用引用。更好的方法是完全排除jQuery,而只引用。