gpt4 book ai didi

javascript - Javascript无法自动计算

转载 作者:行者123 更新时间:2023-12-03 06:37:18 24 4
gpt4 key购买 nike

HTML

<select id="k1" onchange="kes1();" name="kelayakan1" size="1">
<option value="">-- Pilih Salah Satu --</option>
<option value="1">Layak</option>
<option value="0">Tidak Layak</option>
</select>
<br>
<select id="k2" onchange="kes2();" name="kelayakan2" size="1">
<option value="">-- Pilih Salah Satu --</option>
<option value="1">Layak</option>
<option value="0">Tidak Layak</option>
</select>
<br>
<br>
<table style="width:30%;" border="1">
<thead>
<tr>
<th colspan="5">TES</th>
</tr>
<tr>
<th></th>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>
<div id="aa1" class="layak" style="text-align:center;"></div>
</td>
<td>
<div id="aa2" class="layak" style="text-align:center;"></div>
</td>
</tr>
<tr>
<td>2</td>
<td>
<div id="bb1" class="layak" style="text-align:center;"></div>
</td>
<td>
<div id="bb2" class="layak" style="text-align:center;"></div>
</td>
</tr>
<tr>
<td>Result</td>
<td>
<div id="hasil1" style="text-align:center;"></div>
</td>
<td>
<div id="hasil2" style="text-align:center;"></div>
</td>
</tr>
</tbody>
</table>

JS

  function kes1() {
var ap = document.getElementById('k1').value;
var satu = 1;
var nol = 0;
if (ap == "1") {
document.getElementById('aa1').innerHTML = satu;
document.getElementById('aa2').innerHTML = nol;
} else {
document.getElementById('aa1').innerHTML = nol;
document.getElementById('aa2').innerHTML = satu;
}
}
function kes2() {
var bp = document.getElementById('k2').value;
var satu = 1;
var nol = 0;

if (bp == "1") {
document.getElementById('bb1').innerHTML = satu;
document.getElementById('bb2').innerHTML = nol;
} else {
document.getElementById('bb1').innerHTML = nol;
document.getElementById('bb2').innerHTML = satu;
}
}
$(document).ready(function() {
//Iterate through each Textbox and add keyup event handler
$(".layak").each(function() {
$(this).innerHTML(function() {
//Initialize total to 0
var total = 0;
$(".layak").each(function() {
// Sum only if the text entered is number and greater than 0
if (!isNaN(this.value) && this.value.length != 0) {
total += parseFloat(this.value);
}
});
//Assign the total to label
//.toFixed() method will roundoff the final sum to 2 decimal places
$("#hasil1").val(total);
});
});
});

我想计算 A1 和 A2、B1 和 B2 并出现在结果中..

如果1+0=1,如果1+1=2,如果0+0=0

但仍然无法正常工作:(

这个jsfiddle.. https://jsfiddle.net/taraym/57Ltreb0/

你知道怎么解决吗

最佳答案

我用工作版本更新了您的问题 https://jsfiddle.net/57Ltreb0/8/

参见Is there any function like IsNumeric in javascript to validate numbers了解我添加的 func isNumeric

如果对您有帮助,请告诉我。

关于javascript - Javascript无法自动计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com