我有4个输入字段:
所有都是数组输入类型。
目的:
1
1
问题:当两个值同等时,其标记为1
$('#check_bala').click(function(event) {
event.preventDefault();
var inps = document.getElementsByName('credit_bala[]');
var inps1 = document.getElementsByName('apply_amount[]');
var inps2 = document.getElementsByName('balance_rece[]');
var inps3 = document.getElementsByName('apply_amount1[]');
var flag = 0;
var flag1 = 0;
for(i=0;i<inps.length;i++)
{
for(j=0;j<inps1.length;j++)
{
if(inps[i].value <= inps1[j].value)
{
flag=1;
}
}
}
for(i1=0;i1<inps2.length;i1++)
{
for(j1=0;j1<inps3.length;j1++)
{
if(inps2[i1].value <= inps3[j1].value)
{
flag1=1;
}
}
}
console.log(flag);
console.log(flag1);
if(flag== 0 && flag1== 0)
{
console.log("its done");
//$("#apply_form").submit();
}
else
{
alert("Applied Amount is greater than your balance.Try again!");
}
});
答案 0 :(得分:0)
这里的一个大问题是你没有解析数字,但另一个问题是你的循环是错误的。您正在将每个贷方余额与每个申请金额进行比较,而不是仅仅比较同一行中的值。您需要将值作为值行处理。
此外,出于某种原因,当您似乎明白应该使用<=
时,您正在使用<
。
这应该有效:
$('#check_bala').click(function(event) {
event.preventDefault();
var credits = document.getElementsByName('credit_bala[]');
var applys1 = document.getElementsByName('apply_amount[]');
var reces = document.getElementsByName('balance_rece[]');
var applys2 = document.getElementsByName('apply_amount1[]');
var flag1 = false;
var flag2 = false;
var rowCount1 = Math.min(credits.length, applys1.length)
for (var i = 0; i < rowCount1; i += 1) {
if (Number(credits[i].value) < Number(applys1[i].value)) {
flag1 = true;
}
}
var rowCount2 = Math.min(reces.length, applys2.length);
for (var i = 0; i < rowCount2; i += 1) {
if (Number(reces[i].value) < Number(applys2[i].value)) {
flag2 = true;
}
}
console.log(flag1);
console.log(flag2);
if (flag1 || flag2) {
alert("Applied Amount is greater than your balance.Try again!");
} else {
console.log("it's done");
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Credit Balance</th>
<th>Apply Amount</th>
<th>Balance Rec.</th>
<th>Apply Amount</th>
</tr>
</thead>
<tr>
<td><input type="number" name="credit_bala[]" min="0" /></td>
<td><input type="number" name="apply_amount[]" min="0" /></td>
<td><input type="number" name="balance_rece[]" min="0" /></td>
<td><input type="number" name="apply_amount1[]" min="0" /></td>
</tr>
<tr>
<td><input type="number" name="credit_bala[]" min="0" /></td>
<td><input type="number" name="apply_amount[]" min="0" /></td>
<td><input type="number" name="balance_rece[]" min="0" /></td>
<td><input type="number" name="apply_amount1[]" min="0" /></td>
</tr>
</table>
<input type="button" id="check_bala" value="Submit" />
&#13;