Home jquery to submit form only after condition is valid and display alert if not
Reply: 1

jquery to submit form only after condition is valid and display alert if not

Pradip Shrestha
1#
Pradip Shrestha Published in 2017-09-13 08:21:12Z

I have this jQuery code. I want to find out what is causing the alertify.error() to displayed as many times as it executes foreach loop to find the sum of the value of text box. Can somebody help me here.

$(document).ready(function() {
  $("form").submit(function(e) {
    e.preventDefault();
    var sum = claculateTotal();
    if ($("#TotalWeigh").val() != sum) {
      alertify.error('Total weigh should be equal to the sum of weigh of all leaf quality.');
      return false;
    } else {
      this.submit();
    }
  });

  function claculateTotal() {
    var sum = 0;
    $('.leafWeigh').each(function() {
      sum += Number($(this).val());
    });
    return sum;
  }
});

And i have my html code for text boxes as below:

for (int i = 0; i < Model.boughtWeighingModel.Count(); i++)
{
  <tr>
    <td>
      @Html.TextBoxFor(model => Model.boughtWeighingModel[i].LeafQaualityWeight, 
         new { @class = "form-control leafWeigh" })
    </td>
  </tr>
}
levi
2#
levi Reply to 2017-09-13 10:50:38Z

Why do you really need a <form> element?

HTML

<input name="" />
<button id="submit">Submit</button>

JS

submit.onclick = function(){
   // validate and do some ajax...
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.35017 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO