如何将NaN值更改为0?我要删除NaN或将其转换为零。
是否可以使NaN == 0?
$('.srednia').change(function() {
oblicz_srednia_aaa();
oblicz_srednia_bbb();
}).change();
function oblicz_srednia_aaa() {
var Sum = 0,
count = $("[type='checkbox']:checked").length;
$(".AAA").each(function() {
if ($(this).closest('tr').find('.srednia').is(':checked')) {
var value = $('p', this).text();
if (!isNaN(value) && value.length != 0) {
Sum += parseFloat(value);
}
}
});
$('#srednia_AAA').text((Sum / count).toFixed(1));
};
function oblicz_srednia_bbb() {
var Sum = 0,
count = $("[type='checkbox']:checked").length;
$(".BBB").each(function() {
if ($(this).closest('tr').find('.srednia').is(':checked')) {
var value = $('p', this).text();
if (!isNaN(value) && value.length != 0) {
Sum += parseFloat(value);
}
}
});
$('#srednia_BBB').text((Sum / count).toFixed(1));
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Nazwa</th>
<th>Data</th>
<th>A</th>
<th>B</th>
<th></th>
</tr>
</thead>
<tbody>
<tr id="1">
<td>
<p>Firma 1</p>
</td>
<td>
<p>Marzec</p>
</td>
<td class="AAA">
<p>16.4</p>
</td>
<td class="BBB">
<p>25.5</p>
</td>
<td>
<input type="checkbox" class="srednia"> zaznacz
</td>
</tr>
<tr id="2">
<td>
<p>Firma 2</p>
</td>
<td>
<p>Luty</p>
</td>
<td class="AAA">
<p>14.1</p>
</td>
<td class="BBB">
<p>30.2</p>
</td>
<td>
<input type="checkbox" class="srednia"> zaznacz
</td>
</tr>
<tr id="brak">
<td>
<p>Średnia:</p>
</td>
<td>
</td>
<td class="AAA">
<strong><div id="srednia_AAA"></div></strong>
</td>
<td class="BBB">
<strong><div id="srednia_BBB"></div></strong>
</td>
<td>
</td>
</tr>
</tbody>
</table>
您得到NaN
是因为当未进行任何检查时,您将被0
除以。
您可以使用条件表达式在进行除法之前检查count
是否大于0
。
$('.srednia').change(function() {
oblicz_srednia_aaa();
oblicz_srednia_bbb();
}).change();
function oblicz_srednia_aaa() {
var Sum = 0,
count = $("[type='checkbox']:checked").length;
$(".AAA").each(function() {
if ($(this).closest('tr').find('.srednia').is(':checked')) {
var value = $('p', this).text();
if (!isNaN(value) && value.length != 0) {
Sum += parseFloat(value);
}
}
});
$('#srednia_AAA').text((count > 0 ? (Sum / count) : 0).toFixed(1));
};
function oblicz_srednia_bbb() {
var Sum = 0,
count = $("[type='checkbox']:checked").length;
$(".BBB").each(function() {
if ($(this).closest('tr').find('.srednia').is(':checked')) {
var value = $('p', this).text();
if (!isNaN(value) && value.length != 0) {
Sum += parseFloat(value);
}
}
});
$('#srednia_BBB').text((count > 0 ? (Sum / count) : 0).toFixed(1));
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Nazwa</th>
<th>Data</th>
<th>A</th>
<th>B</th>
<th></th>
</tr>
</thead>
<tbody>
<tr id="1">
<td>
<p>Firma 1</p>
</td>
<td>
<p>Marzec</p>
</td>
<td class="AAA">
<p>16.4</p>
</td>
<td class="BBB">
<p>25.5</p>
</td>
<td>
<input type="checkbox" class="srednia"> zaznacz
</td>
</tr>
<tr id="2">
<td>
<p>Firma 2</p>
</td>
<td>
<p>Luty</p>
</td>
<td class="AAA">
<p>14.1</p>
</td>
<td class="BBB">
<p>30.2</p>
</td>
<td>
<input type="checkbox" class="srednia"> zaznacz
</td>
</tr>
<tr id="brak">
<td>
<p>Średnia:</p>
</td>
<td>
</td>
<td class="AAA">
<strong><div id="srednia_AAA"></div></strong>
</td>
<td class="BBB">
<strong><div id="srednia_BBB"></div></strong>
</td>
<td>
</td>
</tr>
</tbody>
</table>