【发布时间】:2014-05-13 20:02:15
【问题描述】:
我有这个脚本来计算表格
$(".mad, .mad2, .mad3, .mad4, .mad5, .mad6").each(function() {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
antal += parseFloat(this.value);
subtotal = antal*30;
$(this).css("background-color", "#ffffff");
}
else if (this.value.length != 0){
$(this).css("background-color", "#e89898");
}
});
$("#antal").html(antal.toFixed(0));
$("#subtotal").html(subtotal.toFixed(0));
如果有人尝试添加一个字母或将其删除并且输入字段变为红色,它似乎工作正常。但是我确实接受空间导致antal和subtotal Show NaN
如何让它同时适用于字母和空格(也不接受空格)?
【问题讨论】:
-
只用空字符串替换空格:
this.value.replace(/ /g,'') -
为什么需要
else if中的if?这不应该只是else吗?
标签: javascript jquery