Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Senso
Nov 4, 2005

Always working
I'm having a weird issue but it's probably something easy I've overlooked.
I have a calendar form with two submit buttons, so users can add/remove date ranges.

I have a debug alert() in there, the whole thing works but the first time, the alert pops once, the second time you click, it pops twice, etc. It looks like I'm adding a handler every time there's a click and they stack or something like that, right? Maybe I'm doing it wrong and I shouldn't bind on the form buttons AND on the submit?

code:
<div id="note"></div>
<form action="" id="add_period" class="add_period">
  <input type="submit" value="Add Period" class="button" id="add_button" />
  <input type="submit" value="Remove Period" class="button" id="rem_button" />
</form>

$("#add_button").click(function() {aurl = "/ajax/add_period/";});
$("#rem_button").click(function() {aurl = "/ajax/rem_period/";});

$(document).ready(function(){
    $("#add_period").submit(function(){
        var str = $(this).serialize();
        
        $.ajax({type: "POST", url: aurl, data: str, success: function(msg){
            $("#note").ajaxComplete(function(event, request, settings){
                alert(aurl);
                var pmsg = JSON.parse(msg);
                ...
            });    
        }
    });
    return false;
    });
});

Adbot
ADBOT LOVES YOU

Senso
Nov 4, 2005

Always working

Lumpy posted:

Each call to .click() ADDS a handler. The little snippet you posted doesn't look like it's adding multiple handlers, but you truncated it so maybe it's somewhere else. To avoid dupe handlers you can do something like:

code:
jQuery('#someEl').unbind('click').click( function(){ //blah } );

Ah, I see my error, it wasn't with the buttons click()! I register ajaxComplete() inside the submit() call, so that one would stack each time an AJAX call was made (for every form submit).

I seem to have fixed it by doing what you did, but for ajaxComplete():
code:
$("#note").unbind('ajaxComplete').ajaxComplete(function(event, request, settings){

  • Locked thread