开发者

Jquery's change() versus Javascript's onchange drawback possible?

开发者 https://www.devze.com 2023-04-12 10:16 出处:网络
I have a form where the user can dynamically add fields (clone and increasing index). I use the sheepit plugin for doing this. You don\'t need to dig into this plugin to answer this question.
  1. I have a form where the user can dynamically add fields (clone and increasing index). I use the sheepit plugin for doing this. You don't need to dig into this plugin to answer this question.

  2. Inside this additonal fields (which will be cloned and increased index) i have a select element which alerts the id of the element (infact it populates different fields depending on input, but faciliated for better illustration). The VERY simpliefied code for that function u find at the end.

The Problem is that if the user adds additional fields (1.), the "change" event from the jquery function does not get binded to the new created field. So (2.) does not work for the new field.

If i开发者_JS百科 would do it with Javascript and add to the html an onchange event, this would work.

How can i do this with jquery?

Here the very simpliefied code:

change()

$(document).ready(function() { 
 $('.viewSelector').change(function() 
    {             
        //GET THE ID            
        var idint = $(this).attr('name');          
        alert(idint);
    });
});

I ONLY want to have this function work also at the new created fields.


Just use live():

$(document).ready(function() { 
  $('.viewSelector').live('change', function() {             
    //GET THE ID            
    var idint = $(this).attr('name');          
    alert(idint);
  });
});

This will bind the event to any new elements added to the DOM that match your selector

0

精彩评论

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

关注公众号