开发者

jQuery Submit Function - How not to have it binded if already binded

开发者 https://www.devze.com 2023-02-27 10:19 出处:网络
I have the following: $(\'#xxxxx\').submit(function() { .... }); This is contained within a script that gets called w jQuery getScript... How can I make sure this bi开发者_如何学Cnd only gets appli

I have the following:

$('#xxxxx').submit(function() {
....
});

This is contained within a script that gets called w jQuery getScript... How can I make sure this bi开发者_如何学Cnd only gets applied once? And or, clear out beforehand?

Thanks


$('#xxxxx').unbind('submit').submit(function() {
....
});


you can use 'unbind' :

$('#xxxxx').unbind('click').
    bind('click', function() {
         ....
    })


Add a class to the form when attaching the handler. When attaching again, just check if the class is already added to the form.

var $xxxx = $('#xxxx')
if (!$xxxx.hasClass('handleradded')) {
  $xxxx.submit(function(){
      ....
  }).addClass('handleradded');
}

Solutions given by others are good ones. I just wanted to give you an alternative. :)

0

精彩评论

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