Javascript 数组不打印/输出 NaN 或 0

标签 javascript html

当 number1 和 number2 为 NaN 或 0 时,如何隐藏/不打印/输出,只需修改以下内容

document.getElementById('inputTextToSave').innerHTML =  parseFloat(number1).toFixed(3) + "," + parseFloat(number2).toFixed(3);

最佳答案

var n1 = parseFloat(number1),
    n2 = parseFloat(number2),
    list = [];

// add values to array if they're not NaN or > 0
if (n1) {
    list.push(n1);
}

if (n2) {
    list.push(n2);
}

// combine values into a string separated with commas    
document.getElementById('inputTextToSave').innerHTML = list.join(",");

关于Javascript 数组不打印/输出 NaN 或 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44216960/

相关文章:

jquery - 如何使用 Jquery 更改 sibling 类?

javascript - 如何在显示另一个元素时隐藏一些元素?

html - 使用 Css calc() 对齐伪元素以在所有屏幕上工作

html - 移动视口(viewport)

html - 如何关闭两个元素之间的空间

javascript - 一页问题中的多 Bootstrap 模式

javascript - 在表单提交之前发送 Ajax 请求

javascript - Bootstrap 导航栏居中对齐

javascript - 从文本框中动态复制内容

javascript - promise 的 promise 到简单的 promise ?