Javascript 计算接受输入字段中的空间

Javascript 计算接受输入字段中的空间,javascript,jquery,Javascript,Jquery,我有这个脚本来计算表单 $(".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); sub

我有这个脚本来计算表单

    $(".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));
如果一个人试图为一个字母添加广告或将其删除,并且输入字段变为红色,那么它似乎可以正常工作。然而,我不接受空间,导致安塔尔和小计显示

如何使其同时适用于字母和空格(也不接受空格)?

尝试使用jquery的trim()方法删除所有空格:

if (this.value.trim().length != 0 && !isNaN(this.value.trim()) ) {

}

只需将空格替换为空字符串:
this.value.replace(//g',)
为什么需要
中的
if
?这不应该是
其他的吗
?可能是重复的谢谢,但是在我的脚本中,我在哪里做广告,我认为你找到了答案?您想用我发布的内容替换您发布的第一条if语句。
if (parseFloat(this.value) == this.value) {