开发者

jquery live function not working for a specific selector

i'm having trouble applying .live function to a specific input tag having a unique id. Am i doing it the wrong way? Any suggestions?

$('input.newQty').live('change', functi开发者_运维问答on(){
}); //works

$('input.newQty[id="'+naam+'"]').live('change', function(){
}); //does not work


How about:

$('#'+ naam).live('change', function() {
    // ...
});

Or if you want to use a name attribute instead of id which must be unique:

$('input.newQty[name="' + naam + '"]').live('change', function() {
    // ...

});


Update: If you must use live events in this case then I would fork out the function that is the event handler and not have it as an anonymous function. This is probably what I would do.

function myChangeHandler(e){
if($(this).attr("id")==naam))
{
    //found the right input, hook this event handler to the input directly and unbind it from all others
    $("input.newQty").die("change",myChangeHandler);
    $(this).bind("change",myChangeHandler);
}
//rest of the code goes here

}

$("input.newQty").live("change",myChangeHandler);

As far as I know live selectors can not be dynamic. As in you can not construct a selector for a live event by joining variables on run time. They need to be evaluated in entirety for them to work.

Hence you can not have a live expression as

$('input.newQty[id="'+naam+'"]').live('change', function(){
}); 

Your best approach will be to use it on the class selector as you have done correctly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜