开发者

SlideDown then call custom function

开发者 https://www.devze.com 2022-12-24 09:56 出处:网络
Having difficulty understanding why my function is being called twice. I\'m trying to detect when a radio button is called (intRev_yes), check if a div is empty, slide down another div and then call

Having difficulty understanding why my function is being called twice.

I'm trying to detect when a radio button is called (intRev_yes), check if a div is empty, slide down another div and then call a custome function which dynamically creates a date field.

if(this.id=="intRev_yes"){
 if($('div#mainField').is(':empty')){
  $('.intRevSections').slideDown('slow',function(){
   current=-1;
   addIrField();
  });
 }   
}

When I alert out at each stage, after getting开发者_C百科 to the end (i.e. addIrField()), the script seems to go back to the slideDown() section and recalls addIrField(). I can't understand why, there are no loops. current is used as an index for the date field.


You are probably calling this from a Dom event that bubbles. See http://en.wikipedia.org/wiki/DOM_events to check if that's the case.


Another case would be:

If you have two elements having class name .intRevSections slideDown would run twice, and callback would fire twice...

Hope this helps, Sinan.


Sinan already discovered the problem, but did not in fact post an answer. the answer is:

Find out why You have two divs (if You create them from JS the other function might be run twice as well)

You may also want to fix the code so that it uses only one div even if You have two of them

 $('.intRevSections:first').slideDown('slow',function(){
   current=-1;
   addIrField();
  });


try this:

if (this.id == "intRev_yes") {
        if ($('div#mainField').is(':empty')) {
            $('.intRevSections').slideDown('slow', function(event) {
                event.stopPropagation();
                current = -1;
                addIrField();
            });
        }
    }


Jquery animate complete worked for me

 $('.intRevSections:first').slideDown('slow',function(){
    complete: addIrField();
 });
0

精彩评论

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