Javascript 计算器在 Firefox 中不工作

标签 javascript jquery html

我有一个用于园林绿化公司的简单面积计算器,它只能在 Chrome、IE 和 Safari 中运行,但不能在 Firefox 中运行。有人可以告诉我问题可能是什么吗?我认为这可能是一个js问题。

这里是工作页面的链接:http://tinyurl.com/calc1test1

这是 html:

  <h4><span>Area Calculator</span></h4>
                            <div class="boxInfo contactForm">

                                      <div style="display:none"><input type="checkbox"         name="CampaignList_64585" checked="checked" value="on" /></div>
                                     <div>
                                         <label>Length:</label>
                                        <input type=text name="square_length" size="3" onChange="get_cubicyards();">
                                    </div>
                                    <div>
                                        <label>Width:</label>
                                        <input type=text name="square_width" size="3" onChange="get_cubicyards();">
                                    </div>
                                    <div>
                                        <label>Depth:</label>
                                         <input type=text name="square_depth" size="3" onChange="get_cubicyards();">
                                    </div>

                                <div class="calcbutton">

                                        <input id="contactSubmit" class="calcsubmit"  type="button" value="Get Amounts" onClick="get_cubicyards();">
                                </div>

                                    <div>
                                        <label>Cubic Yards:</label>
                                        <input type=text name="square_cuyard" onFocus="calculate_totals();this.blur();">
                                    </div>

这是 JS:

 <script language="Javascript">
    function isValidInput(strString)
    {
        //String of valid characters
        var strValidChars = '0123456789.';
        var strChar;
        var blnResult = true;
        var decimalCount = 0;

        if (strString.length == 0) return false;

        //Test strString consists of valid characters listed above
        for (i = 0; i < strString.length && blnResult == true; i++)
        {   strChar = strString.charAt(i);
            if (strValidChars.indexOf(strChar) == -1)
            {   blnResult = false; }
            if (strChar == '.')
            { decimalCount++; }
        }

        if (decimalCount > 1)
        { blnResult = false; }

        return blnResult;
    }

    function get_cubicyards()
    {   var cubicyards;

                if(  (eval('document.all.square_depth.value')) && (!isValidInput(eval('document.all.square_depth.value')))  )
                {   alert('You must enter only numeric values.'); 
                    eval('document.all.square_cuyard.value = \'\'');
                    return false; 
                }
                if(  (eval('document.all.square_length.value')) && (!isValidInput(eval('document.all.square_length.value')))  )
                {   alert('You must enter only numeric values.');                   
                    eval('document.all.square_cuyard.value = \'\'');
                    return false; 
                }
                if(  (eval('document.all.square_width.value')) && (!isValidInput(eval('document.all.square_width.value')))  )
                {   alert('You must enter only numeric values.'); 
                    eval('document.all.square_cuyard.value = \'\'');
                    return false; 
                }

                //Only make calculations if there are valid values specified for ALL necessary fields
                if (  (eval('document.all.square_depth.value')) && (eval('document.all.square_length.value')) && (eval('document.all.square_width.value'))  )
                {   cubicyards = (eval('(document.all.square_depth.value * document.all.square_length.value * document.all.square_width.value)/324')*1000)/1000;
                    eval('document.all.square_cuyard.value = '+cubicyards);
                    return true;
                }


    }
</script>

最佳答案

您正在使用 document.all 这不符合 w3c 标准。您可以将 id 添加到输入字段,然后使用 w3c 标准获取它们的值:

document.getElementById('[id name here]')

关于Javascript 计算器在 Firefox 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16021120/

相关文章:

javascript - 获取 https ://ipinfo. io/401(未经授权)

javascript - jQuery 淡入淡出效果未按预期工作

AngularJS 高度均衡器隐藏的 div

javascript - 在 JSP 之间共享动态客户端和服务器端内容

javascript - 如何在 Jinja2 占位符中包含 javascript 占位符?

javascript - 如何调用 jquery 插件中的这个函数?

javascript - 在javascript中将数字美化为最接近的数字,末尾加零

javascript - 使用 jquery/ajax 自动刷新特定 div 并加载图像

jquery - 每个循环的条件给出错误的结果

javascript - Jquery 删除或更改 JSON 根元素