[英]jquery to submit form only after condition is valid and display alert if not
我有这个jQuery代码。 我想找出导致alertify.error()
显示的次数与执行foreach循环以查找文本框的值之和一样多。 有人可以帮我吗?
$(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;
}
});
我有如下文本框的html代码:
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>
}
为什么真的需要<form>
元素?
的HTML
<input name="" />
<button id="submit">Submit</button>
JS
submit.onclick = function(){
// validate and do some ajax...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.