Saturday, 15 March 2014

javascript - JQuery Ajax working only once on pressing submit and the next time the page refreshes -



javascript - JQuery Ajax working only once on pressing submit and the next time the page refreshes -

i have form on webpage , uses jquery ajax. now, when press submit first time, code executed , info submitted , message appears. problem is, if there message "you cannot leave field blank", , right details , press submit, page reloads should not happen. jquery -

$(document).ready(function () { $('form').submit(function () { $('#content').fadeout(100, function () { $(this).html(' <img src="//mywebsite.com/spinner.gif"/> <div style="display:inline;color:#1faeff">loading...</div> ').fadein(100); }); $.get('submit.server', $(this).serialize(), function (data) { $('#content').html(data); }); homecoming false; }); });

i tried replacing $('form').submit(function(){ $('form').live('submit', function() { doesn't work. please help! using latest jquery version website. edit - tried on. same thing happens. also, form has multiple forms handled same script. 1 of forms -

<form action="submit.server" method="get"> <label for="form" style="color:#1faeff;">change emailaddr : <br> </label> <div style="float:left;margin-top:20px"> <input type="email" class="forminput" name="gotemail" /> <br /> <input type="hidden" value="email" name="data" /> <button name="submit" value="email" class="action" style="width:150px;height:70px">submit</button> </div> </form>

as stated in comments

@soyuka: live has been depreceated since 1.9 utilize on instead ;)

this right syntax .on():

$(document).on("submit", "form", function() { });

here useful links understand difference between .live , .on: jquery 1.7+ .on() vs .live() review

what's difference between jquery .live() , .on()

and intergrated code:

$(document).ready(function () { $(document).on("submit", "form", function() { $('#content').fadeout(100, function () { $(this).html(' <img src="//mywebsite.com/spinner.gif"/> <div style="display:inline;color:#1faeff">loading...</div> ').fadein(100); }); $.get('submit.server', $(this).serialize(), function (data) { $('#content').html(data); }); homecoming false; }); });

javascript jquery

No comments:

Post a Comment