开发者

make append happen only the first time

开发者 https://www.devze.com 2023-04-06 03:41 出处:网络
i have on a list a button that fire this event: js: $(this).parents(\'tr\') .find(\'.class\') .append(\"<input type=\'text\' size=\'5\'/>\");

i have on a list a button that fire this event:

js:

$(this).parents('tr')
       .find('.class')
       .append("<input type='text' size='5'/>");

its possible to开发者_StackOverflow社区 make this append happen just in the first time?

like a return false, dont know.

Thanks!


Assuming that this behavior is happening on a click event, you can use one. E.g.,

$('#my-button').one('click', function(e) {
    e.preventDefault();
    $(this).parents('tr')
       .find('.class')
       .append("<input type='text' size='5'/>");
});


Edit: using delegate:

var selector = '#my-button',
    handler = function(e) {
        // prevent default behavior
        e.preventDefault();
        // make sure this only runs once
        $(document).undelegate(selector, 'click', handler);
        // actual behavior
        $(this).parents('tr')
           .find('.class')
           .append("<input type='text' size='5'/>");
    };
$(document).delegate(selector, 'click', handler);


try the one() method instead of click().

0

精彩评论

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