开发者

jQuery - Bind action to parent form, determine what child triggered the bind event

开发者 https://www.devze.com 2023-04-03 11:02 出处:网络
I have a form that has multiple input fields. I\'m applying a bind() to this form as so: $(\"#form-id\").bind(\'keyup change\', function(e) {

I have a form that has multiple input fields. I'm applying a bind() to this form as so:

$("#form-id").bind('keyup change', function(e) {
   //Run some other code
   alert('here');
});

I would like to determine the ID of which form field triggered the bind event to fire. I've tried working with the 开发者_JAVA百科Event object sent on the function, however the information doesn't seem to reside there. For example, e.relatedTarget is null.

Is is possible to determine what child element of the form triggered the bind event to be triggered?


Use the event target:

$("#form-id").bind('keyup change', function(e) {
    var targetId = e.target.id;
});

Here's a working example.


http://api.jquery.com/event.target/ -- event.target should be what you want.


Perhaps something like this?

$('#form-id input').bind('keyup change'), function(e) {
    alert($(this).attr('id'));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消