我有一個美化公司的簡單的面積計算器,它只在鉻,IE瀏覽器和Safari,但不是在Firefox中工作。有人能告訴我問題可能是什麼嗎?我想這可能是一個JS問題。Javascript計算器不能在火狐工作
這裏是一個工作頁面的鏈接: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 = '.';
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>
爲什麼使用'eval'? – Niels
你甚至檢查過控制檯嗎? 'TypeError:document.all是未定義的'。重複解決方案在這裏:http://stackoverflow.com/questions/4820440/error-with-javascript-in-firefox – jchapa
你可能會更好使用jQuery而不是依靠瀏覽器特定的屬性,如「document.all」。另外'eval'是邪惡的,所以要小心:) http://jslinterrors.com/eval-is-evil/ – cfs