我在模糊调用中添加所有文本框值。我在一个文本框中给出了一些数字。当它尝试添加其他文本框值的值时,它会抛出NaN。
这是我的代码
function sumOfIncome() {
var sum = 0;
$('.add').each(function() {
sum += parseInt(this.value);
console.log(sum)
});
$('#netPay').val(sum);
}
$('.add').blur(function() {
sumOfIncome();
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group row">
<div class="col-md-3">
<label for="DA" class="col-form-label">DA</label>
</div>
<div class="col-sm-2">
<input type="number" class="form-control add" id="da" placeholder="DA">
</div>
</div>
<div class="form-group row">
<div class="col-md-3">
<label for="HRA" class="col-form-label">HRA</label>
</div>
<div class="col-sm-2">
<input type="number" class="form-control add" id="hra" placeholder="HRA">
</div>
</div>
&#13;
我在哪里做错了?
答案 0 :(得分:1)
最简单的解决方案是将总和行更改为sum += parseInt(this.value) || 0;
。这样,如果该字段为空并返回NaN
值0
,则会使用function sumOfIncome() {
var sum = 0;
$('.add').each(function() {
sum += parseInt(this.value) || 0;
console.log(sum)
});
$('#netPay').val(sum);
}
$('.add').blur(function() {
sumOfIncome();
})
。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group row">
<div class="col-md-3">
<label for="DA" class="col-form-label">DA</label>
</div>
<div class="col-sm-2">
<input type="number" class="form-control add" id="da" placeholder="DA">
</div>
</div>
<div class="form-group row">
<div class="col-md-3">
<label for="HRA" class="col-form-label">HRA</label>
</div>
<div class="col-sm-2">
<input type="number" class="form-control add" id="hra" placeholder="HRA">
</div>
</div>
Private Sub CommandButton1_Click()
ActiveWorkbook.Sheets.Add Before:=Worksheets(Worksheets.Count)
i = Sheet1.Cells(8, 6).Value
CandidateCount = Sheet1.Cells(11, 8).Value
CandidateState = CandidateCount + 1
SheetName = Sheet1.Cells(13, 8).Value
OrSheetName = Sheet1.Cells(13, 8).Value
v = 0
For Each Sheet In Worksheets
If SheetName = Sheet.Name Then
SheetName = OrSheetName & "_Ballot" & v
v = v + 1
End If
Next Sheet
ActiveSheet.Name = SheetName
For x = 1 To i
b = 15 + x
Subdistrict = Cells(b, 2).Value
MembersPresent = Cells(b, 6).Value
ActiveSheet.Cells(1, (x + 1)).Value = Subdistrict
For a = 2 To 6
ActiveSheet.Cells(a, x + 1).Value = 0
With ActiveSheet.Range(ActiveSheet.Cells(a, x + 1), ActiveSheet.Cells(a, x + 1)).Validation
.Add Type:=xlValidateWholeNumber, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:="0", Formula2:=MembersPresent
.InputTitle = "Integers"
.ErrorTitle = "Integers"
.InputMessage = "Enter an integer from 0 to " & MembersPresent
.ErrorMessage = "You must enter a number no less than 0 and no greater than the number of members in attendance: " & MembersPresent
End With
Next a
Next x
For b = 1 To CandidateCount
Named = "Candidate Name" & " " & b
ActiveSheet.Cells((b + 1), 1).Value = Named
Next b
ActiveSheet.Cells(CandidateCount + 1, 1).Value = "Raw Vote Totals"
答案 1 :(得分:0)
使用此$.isNumeric()
jsfiddle:
HTML:
<div class="form-group row">
<div class="col-md-3"><label for="DA" class="col-form-label">DA</label></div>
<div class="col-sm-2"> <input type="number" class="form-control add" id="da" placeholder="DA">
</div>
</div>
<div class="form-group row">
<div class="col-md-3"><label for="HRA" class="col-form-label">HRA</label></div>
<div class="col-sm-2"> <input type="number" class="form-control add" id="hra" placeholder="HRA">
</div>
</div>
<div id="netPay">
</div>
Jquery的:
function sumOfIncome() {
var sum = 0;
$('.add').each(function(){
var val = $(this).val();
if($.isNumeric(val))
{
sum += parseInt(val);
console.log(sum)
}
});
$('#netPay').text(sum);
}
$('.add').blur(function(){
sumOfIncome();
});