javascript - Number of ajax call increases every time submission of the form -
i using below code submit form using ajax.
first : first time, ajax doesn't call, , have click again. after sec click, ajax called..
second : every time submit, number of ajax phone call increases.
for example, if submit first time, called once. then, if click on sec time, calls 2 time. , on 3 time, calls 3 time......
below code. please help me out here.. beginner @ jquery.
i have used bootstrap validate on form submission.
class="snippet-code-js lang-js prettyprint-override">$("#subscriptionform").validate( { rules: { subemail: { required: true, email: true } }, submithandler: function (form) {//alert("dfd"); $('#subscribebtn').on('click', function () { var $btn = $(this); $btn.button('loading'); //alert("dfadsfs"); $.ajax({ type: $(form).attr('method'), url: baseurl+"contactus/subscribe", data: $(form).serialize(), success: function (status) { $('#subinputid').val(''); if(status==1) { alert("you have subscribed successfully."); } else { alert("you have alerady subscribed."); } console.log(status); //alert(status); homecoming false; } }); settimeout(function () { $btn.button('reset'); }, 1000); }); } });
class="snippet-code-html lang-html prettyprint-override"><form method="post" id="subscriptionform" name="subscriptionform" action="<?= baseurl.'contactus/subscribe' ?>"> <div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 padding0 text-right"> <div class="newsletter-label"> newsletter </div> <div> <input class="email-input" id="subinputid" name="subemail" type="text" placeholder="enter email" > </div> </div> <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 padding0"> <button id="subscribebtn" data-loading-text="subscribing..." autocomplete="off" class="btn-subscrib" type="submit" name="subscribe" value="subscribe">subscribe</button> </div> </form>
put unbind before line below:
$('#subscribebtn').unbind('click'); $('#subscribebtn').on('click', function () { var $btn = $(this);
will work fine here, why happens when phone call validate, click binding 1 time again , again, unbind click event , 1 time again bind event.
javascript php ajax forms twitter-bootstrap
No comments:
Post a Comment