我正在提交一份基本上是购买发票的表格。在提交表单之前,我想执行检查,确保用户在文本框中输入的金额不超过账单总额。例如,如果账单总额为 300,并且在“已付款金额”文本框中用户意外输入 3000,则它应该显示错误消息,这是我的代码:
$("#mainform").submit(function() {
var total = $("#gtotal").val();
var paid = $("#paid").val();
alert(paid);
alert(total);
if(paid > total)
{
alert("Amount can't be greater than total");
return false;
}
return true;
});
当我提醒已付款和总金额时,它们显示正确的值,但如果条件有时不起作用,它会提交表单,如果条件不满足,有时我什至不满足条件
最佳答案
试试这个,它可能会起作用:
var total = parseInt($("#gtotal").val());
var paid = parseInt($("#paid").val());
关于javascript - 在 jquery 中提交表单之前检查条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43571744/