프로젝트로 저는 "베어 본"예산 웹 응용 프로그램을 구축 중입니다. 나는 소득원과 금액을 기입하기 위해 많은 의견이있는 양식을 가지고있다. 값이 0 인 배열에 빈 입력을 추가합니다.
<form>
<div class="form-group">
<input type="number" class="form-control" id="amount1" placeholder="Enter Amount" name="incAmount">
<input type="number" class="form-control" id="amount2" placeholder="Enter Amount" name="incAmount">
<input type="number" class="form-control" id="amount3" placeholder="Enter Amount" name="incAmount">
<input type="number" class="form-control" id="amount4" placeholder="Enter Amount" name="incAmount">
</div>
</form>
<form>
<div class="form-group">
<input type="text" class="form-control" id="incomeSrc1" placeholder="Enter Source" name="incomeSrc">
<input type="text" class="form-control" id="incomeSrc2" placeholder="Enter Source" name="incomeSrc">
<input type="text" class="form-control" id="incomeSrc3" placeholder="Enter Source" name="incomeSrc">
<input type="text" class="form-control" id="incomeSrc4" placeholder="Enter Source" name="incomeSrc">
</div>
</form>
그리고
는 내가 함께 추가 소득의 가장 큰 원인을 확인 수 amountArr 그래서 배열로 모든 입력을 넣어했습니다.이
내 최신 시도했다 :var incAmount = document.getElementsByName("incAmount");
var amountArr = [];
//Income input function
for (var index = 0; index < incAmount.length; index++) {
incAmount[index].addEventListener("input", add3);
function add3() {
for (var i = 0; i < incAmount.length; i++) {
amountArr[i] = parseFloat(incAmount[i].value);
if (incAmount[i].value === NaN) {
amountArr[i] = 0;
};
};
};
};
목표는 입력이 작성되지 않은 경우 다음 배열의 값은 0 그러나 내 모든 시도 중 하나와 같은 오류를 던졌다 될 것입니다 -
incAmount[i].value is undefined
또는 배열을 NaN으로 채웠다. 비교했을 때
는 또한
if (amountArr[i].value !== NaN)
그 중
if (isNaN(amountArr[i]))
없음이 참 반환 (실행 계속되는 코드)
왜 amountArr하지 않거나 사실 빈 incAmount의 reurn 시도 NaN/null/undefined에?
'incAmount'는 어디에 정의되어 있습니까? – epascarello
확인하지 않으려면 0으로 채울 수 있습니다. 그런 다음 NaN이 아니면 값을 삽입하십시오. –
var incAmount = document.getElementsByName ("incAmount"); – Chuck