简体   繁体   中英

Form submitting multiple times

I have been using jQuery 1.8.3, and have two forms and two radio buttons, form will show when click on the radio buttons. what i am doing here is when click on save button, i will check which radio is checked, depends on that form will submit, the problem is after one form submit, that request going as ajax, so i am not refresh the page after one submit, when click on the save button, two times the form will submit, and next time 4 times ...?

js code

$(".save").live("click", function() {
  if ($("#b1").is(":checked")) {
    $("#lForm1").submit();
  }
  if ($("#b2").is(":checked")) {
    $("#lForm2").submit();
  }
});

$('input[type="radio"]').click(function() {
  //here toggle the form
});

html

<input type="radio" name="optionsRadios" id="b1" checked>
<input type="radio" name="optionsRadios" id="b2" >
<%= form_tag '/contact/create_check', method: :post, remote: true, id: 
'lForm1' %>
<%= form_tag '/contact/create_count', method: :post, remote: true, id: 
'lForm2' %>
<input type="button" class="btn btn-success save" value="Save" />

Please do something like this instead

$(function() {
  $("#lForm1").on("submit", function(e) {
    e.preventDefault(); // stop submission
    if ($("#b1").is(":checked")) {
      $.ajax({url:$("#lform1").prop("action"),data:$("#lform1").serialize()});
    }
    else if ($("#b2").is(":checked")) {
      $.ajax({url:$("#lform2").prop("action"),data:$("#lform2").serialize()});
    }
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM