首页 > 解决方案 > 当我输入长度为 4 的值但返回所有其他值的正确长度时,我的 if 语句返回 0 作为变量的长度

问题描述

我试图将在表单的一个字段中输入的值限制为 4 位数字。如果它大于或小于 4 位,它应该发出警报,并让您知道您没有正确的位数(我目前在警报中有 major.length,所以我可以看到它正在确定的长度)。当我输入多于或少于 4 位的值时,它可以工作并返回我输入的值的正确长度,但是当我输入 4 位的值时,它返回 0 作为长度,从而验证我的 if 语句,我不知道为什么. 我对 javascript 很陌生,所以对于任何非最佳代码,我深表歉意。

function digitCount() {
  for (i=1; i < 7; i++) {
    major = document.getElementById("major" + i.toString()).value;
    minor = document.getElementById("minor" + i.toString()).value;
    sub1 = document.getElementById("sub1" + i.toString()).value;
    sub2 = document.getElementById("sub2" + i.toString()).value;

    if (major.length != 4 || minor.length != 4 || sub1.length != 4 || sub2.length != 4) {
      alert(major.length);
      return false;
    }
  }
  return true;
}
<table border="1">
<tbody><tr><td colspan="6" align="center"><b>Journal Voucher</b></td></tr>
<tr><td colspan="4"><b>SRN:</b><input type="text" id="SRN" size="4" value="8010"> (4 digits)</td><td colspan="2"><b>Fiscal Month: <input type="text" id="month" size="2" value="11"> Must be 1,2,3,...12</b></td></tr>
<tr><td colspan="6" align="center"><b>Journal Entries</b></td></tr>
<tr><td colspan="4"><b><center>General Ledger Account Number</center></b></td>
<td align="center" rowspan="2"><b>Transaction<br>Amount</b></td>
<td align="center" rowspan="2"><b>Description (max. 50 characters)</b></td>
</tr>
<tr>
<td align="center"><b>Major</b></td><td align="center"><b>Minor</b></td><td align="center"><b>Sub<br>Acct 1</b></td><td align="center"><b>Sub<br>Acct 2</b></td>
</tr>
<tr>
<td><input type="text" id="major1" size="4" value="1000"></td><td><input type="text" id="minor1"size="4" value="1000"></td><td><input type="text" id="sub11" size="4" value="1000"></td><td><input type="text" id="sub21" size="4" value="0000"></td><td align="right"><input type="text" id="balance1" size="12" align="right" value="1234.56"></td><td><input type="text" id="desc1" size="50" value="Record Store Sales"></td>
</tr>
<tr>
<td><input type="text" id="major2" size="4" value="4000"></td><td><input type="text" id="minor2" size="4" value="1000"></td><td><input type="text" id="sub12" size="4" value="0000"></td><td><input type="text" id="sub22" size="4" value="0000"></td><td align="right"><input type="text" id="balance2" size="12" align="right" value="-1000.00"></td><td><input type="text" id="desc2" size="50"></td>
</tr>

<tr>
<td><input type="text" id="major3" size="4" value="4000"></td><td><input type="text" id="minor3" size="4" value="2000"></td><td><input type="text" id="sub13" size="4" value="0000"></td><td><input type="text" id="sub23" size="4" value="0000"></td><td align="right"><input type="text" id="balance3" size="12" value="-234.56"></td><td><input type="text" id="desc3" size="50"></td>
</tr>

<tr>
<td><input type="text" id="major4" size="4"></td><td><input type="text" id="minor4" size="4"></td><td><input type="text" id="sub14" size="4"></td><td><input type="text" id="sub24" size="4"></td><td align="right"><input type="text" id="balance4" size="12"></td><td><input type="text" id="desc4" size="50"></td>
</tr>

<tr>
<td><input type="text" id="major5" size="4"></td><td><input type="text" id="minor5" size="4"></td><td><input type="text" id="sub15" size="4"></td><td><input type="text" id="sub25" size="4"></td><td align="right"><input type="text" id="balance5" size="12"></td><td><input type="text" id="desc5" size="50"></td>
</tr>

<tr>
<td><input type="text" id="major6" size="4"></td><td><input type="text" id="minor6" size="4"></td><td><input type="text" id="sub16" size="4"></td><td><input type="text" id="sub26" size="4"></td><td align="right"><input type="text" id="balance6" size="12"></td><td><input type="text" id="desc6" size="50"></td>
</tr>
<tr>
<td colspan="4"><b>Sum of Transaction Amounts</b></td><td align="right"><input type="text" id="sum" size="12" value="0.00"></td><td> <b> (Must sum to 0.0)</b></td>
</tr>
<tr><td colspan="6" align="center"><input type="button" onclick="validate()" value="Validate and Submit"></td></tr>
</tbody></table>

标签: javascriptoperators

解决方案


推荐阅读