Javascript计算器在firefox中不工作
我有一个简单的面积计算器,用于一家景观公司,它只在chrome、IE和Safari中工作,在firefox中不工作。有人能告诉我问题是什么吗?我想这可能是js的问题 以下是指向工作页面的链接: 以下是html:Javascript计算器在firefox中不工作,javascript,jquery,html,Javascript,Jquery,Html,我有一个简单的面积计算器,用于一家景观公司,它只在chrome、IE和Safari中工作,在firefox中不工作。有人能告诉我问题是什么吗?我想这可能是js的问题 以下是指向工作页面的链接: 以下是html: <h4><span>Area Calculator</span></h4> <div class="boxInfo contactForm">
<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>
函数isValidInput(strString)
{
//有效字符字符串
var strValidChars='0123456789';
var-strChar;
var blnResult=真;
var小数=0;
if(strString.length==0)返回false;
//测试字符串由上面列出的有效字符组成
对于(i=0;i1)
{blnResult=false;}
返回blnResult;
}
函数get_cubicyards()
{var cubicyards;
if((eval('document.all.square\u depth.value'))和(!isValidInput(eval('document.all.square\u depth.value'))
{alert('必须只输入数值');
eval('document.all.square\u cuyard.value=\'\'''''');
返回false;
}
if((eval('document.all.square\u length.value'))和(!isValidInput(eval('document.all.square\u length.value')))
{alert('必须只输入数值');
eval('document.all.square\u cuyard.value=\'\'''''');
返回false;
}
if((eval('document.all.square\u width.value'))&(!isValidInput(eval('document.all.square\u width.value'))
{alert('必须只输入数值');
eval('document.all.square\u cuyard.value=\'\'''''');
返回false;
}
//仅当为所有必要字段指定了有效值时才进行计算
如果((eval('document.all.square\u depth.value'))&&(eval('document.all.square\u length.value'))&(eval('document.all.square\u width.value'))
{cubicyards=(eval('(document.all.square\u depth.value*document.all.square\u length.value*document.all.square\u width.value)/324')*1000;
eval('document.all.square_cuyard.value='+立方体);
返回true;
}
}
您正在使用文档。所有这些都不在w3c标准中。您可以向输入字段添加id,然后使用w3c标准获取其值:
document.getElementById('[id name here]')
if((eval('document.all.square\u depth.value'))和(!isValidInput(eval('document.all.square\u depth.value')))
快速的回答是Firefox不支持
但是,嗯,很长的答案是这段代码并不完整。您可能需要插入表单
标记,这样您就可以执行交叉浏览器代码,如
<form name='myForm'>
<h4>Area Calculator</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>
</div>
</form>
老实说,对于交叉浏览器un友好的代码,这种奇怪的eval
用法被其他人指出,不,潜在的div
滥杀,嵌套在h4
中的免费span
,等等,你可能真的会从放慢速度和遵循一些介绍性教程中获益(不是很好,但这是一个开始)在问太多问题之前
我们都从某个地方开始!祝你好运。Firefox控制台显示此错误:
[10:26:13.365] TypeError: document.all is undefined @ http://kynock.wildwingdesign.com/calctest:72
您不能在Firefox中使用
文档。所有的都是在Firefox中使用的。这里真正的问题是您的javascript设计得很糟糕。这里不需要eval()
,现在应该需要文档。所有的。只需给每个输入一个id
值,然后使用documentGetElementById('yourId')).value
以获取输入中存储的值。为什么使用eval
?您甚至检查过控制台吗?类型错误:document.all未定义。
这里的重复解决方案:您可能最好使用jQuery,而不是依赖于浏览器特定的属性,如document.all
。另外eval
也未定义邪恶,所以小心:)
[10:26:13.365] TypeError: document.all is undefined @ http://kynock.wildwingdesign.com/calctest:72