我对Javascript/Jquery很陌生。下面是我的代码,只要输入中有3个当前值,就可以计算平均值(A)值。

以下是jquery代码
$(".q-value, .e-value, .t-value").click(function(){
let currentRow = $(this).closest('tr');
let EValue = parseFloat(currentRow.find('.e-value').val());
let QValue = parseFloat(currentRow.find('.q-value').val());
let TValue = parseFloat(currentRow.find('.t-value').val());
currentRow.find('.a-value-core').val((EValue + QValue + TValue ) / 3);
currentRow.find('.a-value-support').val((EValue + QValue + TValue ) / 3);
currentRow.find('.a-value-research').val((EValue + QValue + TValue ) / 3);});我想设置一个平均值,即使Q、E或T输入是空的,但是A值是不计算的。当Q、E或T值为空时。
例如。

发布于 2020-07-18 04:31:37
试一试
Number(currentRow.find('.*-value').val());而不是
parseFloat(currentRow.find('.*-value').val());当在下拉列表中没有选择值时,.val()返回一个空字符串,null,而parseFloat("")返回NaN。当您试图将NaN添加到任意数字时,返回的答案仍然是NaN,您的结果字段仍然是空的。
更多的注意事项:
。
PS:使用console.log(变量)进行快速调试,并在浏览器Devtools中的控制台中查找错误消息,并与您的问题共享相同的信息,这是一个好主意。
发布于 2020-07-19 03:20:10
你好,AlwaysaLearner注意到你的笔记。至于计算,我必须更改计算逻辑,方法是为Q、E或T值创建一个计数器!== 0然后在if else语句中设置计数器+1。下面是代码
//COMPUTE THE AVERAGE PER ROW
$(".q-value, .e-value, .t-value").change(function(){
let currentRow = $(this).closest('tr');
let EValue = Number(currentRow.find('.e-value').val());
let QValue = Number(currentRow.find('.q-value').val());
let TValue = Number(currentRow.find('.t-value').val());
let counter = 0
if (QValue !== 0){
counter = counter + 1
}
if (EValue !== 0){
counter = counter + 1
}
if (TValue !== 0){
counter = counter + 1
}
currentRow.find('.clerical-value-core').val((EValue + QValue + TValue ) / Number(counter));
currentRow.find('.technical-value-core').val((EValue + QValue + TValue ) / Number(counter));
currentRow.find('.a-value-support').val((EValue + QValue + TValue ) / Number(counter)); });现在它工作得很好。我还修改了处理程序.change,并为每个字段应用了Number函数。
https://stackoverflow.com/questions/62963732
复制相似问题