簡體   English   中英

嘗試建立會計計算器,但JavaScript在Safari / Firefox中不起作用

[英]Trying to build accountants calculators but javascript doesn't work on safari/firefox

我一直在為會計的一些計算器編寫一些代碼。 在Internet Explorer中似乎可以正常工作:您輸入數字並將其全部添加,但是在Safari和Firefox中,好像沒有識別JavaScript文件,只是向上滾動到頁面頂部。

任何幫助將不勝感激。

JavaScript:

function Calculation()
{
var error_flag = false;
var error_string = "";

if (isNaN(document.calcfuel.mileage.value))
    {
    error_flag = true
    error_string = error_string + "Non numeric mileage.\n"
    }

if (isNaN(document.calcfuel.mpg.value))
    {
    error_flag = true
    error_string = error_string + "Non numeric miles per gallon.\n"
    }

if (isNaN(document.calcfuel.fuelprice.value))
    {
    error_flag = true
    error_string = error_string + "Non numeric fuel price.\n"
    }

if (error_flag)
    alert(error_string);
else
    {
    var fuelcost = ((document.calcfuel.mileage.value / document.calcfuel.mpg.value) * 4.54 * document.calcfuel.fuelprice.value) / 100;
    fuelcost = (parseInt(fuelcost * 100)) / 100;

    if (isNaN(fuelcost))
        {
        error_string = "Incorrect data entered\n";
        alert(error_string);
        }
    else
        {
        document.calcfuel.output_value.value=fuelcost;
        return false;
        }
    }
}

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" class="pageCalcPFuelcost" lang="en" xml:lang="en" id="sharedContentCalculators">
<head>
<script src="C:\Users\Faye\Desktop\calcs\calcs\fuel_cost.js" type="text/javascript">
</script>
<div id="calculator">
<form name="calcfuel" id="calcfuel">

<table border="0" cellpadding="0" cellspacing="0" width="428" class="bgColorContent">
    <tr>
        <td class="cornerTopLeft"><img src="/images/spacer.gif" alt="" width="8" height="32" border="0" /></td>
        <td class="bgTop">How much do you spend on fuel in a year?</td>
        <td class="cornerTopRight"><img src="/images/spacer.gif" alt="" width="8" height="32" border="0" /></td>
    </tr>
    <tr>
        <td class="bgLeft"><img src="/images/spacer.gif" alt="" width="8" height="1" border="0" /></td>
        <td class="bgColorContent"><br /><strong>With the fluctuating cost of fuel seemingly an ever-present news item, we thought that using our fuel cost calculator would show how much you are now spending on fuel.<br /><br /> Enter the number of miles you expect to drive in the next twelve months, the cost of fuel, less <acronym title="Value Added Tax">VAT</acronym> if you are <acronym title="Value Added Tax">VAT</acronym> registered and the mpg for your vehicle.</strong></td>
        <td class="bgRight"><img src="/images/spacer.gif" alt="" width="8" height="1" border="0" /></td>
    </tr>
    <tr>
        <td class="bgLeft"><img src="/images/spacer.gif" alt="" width="8" height="1" border="0" /></td>
        <td class="bgColorContent">
            <table align="center" cellpadding="0" cellspacing="5" border="0" width="360">
                <tr> 
                    <td align="right" class="bgColorContent">


<tr>
    <td class="borderhorizontal" colspan="2"><img src="/images/spacer.gif" alt="" width="1" height="1" border="0" /></td>
</tr>
<tr>
    <td class="bgColorContent" colspan="2"><img src="/images/spacer.gif" alt="" width="1" height="5" border="0" /></td>
</tr>
<tr>
    <td class="bgColorContent" align="right"><label for="miles">Enter the number of miles per year you drive :</label></td>
    <td class="bgColorContent"><input type="text" class="form" id="miles" name="mileage" value="12000" size="10" maxlength="6"></td>
</tr>

<tr>
    <td class="bgColorContent" align="right"><label for="price_litre">Enter your normal price for a litre of fuel in pence:</label></td>
    <td class="bgColorContent" align="right" valign="bottom"><input type="text" class="form" id="price_litre" name="fuelprice" value="135" size="10" /></td>
</tr>

<tr>
    <td class="bgColorContent" align="right"><label for="mpg">Enter the <acronym title="Miles per Gallon">MPG</acronym> you average in your vehicle :</label></td>
    <td class="bgColorContent"><input type="text" id="mpg" class="form" name="mpg" value="35" size="10" /></td>
</tr>
<tr>
    <td class="bgColorContent" colspan="2"><img src="/images/spacer.gif" alt="" width="1" height="5" border="0" /></td>
</tr>
<tr>
    <td colspan="2" align="right" class="bgColorContent"><input class="button" type="reset" name="Reset" value="Reset" />&nbsp;&nbsp;<input class="button" type="button" name="Calculate" value="Calculate" OnClick="Calculation()">
    </td>
</tr>
<tr>
    <td class="bgColorContent" colspan="2"><img src="/images/spacer.gif" alt="" width="1" height="5" border="0" /></td>
</tr>
<tr> 
    <td align="right" class="bgColorContent"><strong><label for="todays_money">Fuel expenditure per year </label></strong></td>
    <td align="right" valign="bottom" class="bgColorContent"><input type="text" id="todays_money" name="output_value" size="10" class="form" /></td>
</tr>



<tr>
    <td class="bgColorContent" colspan="2"><img src="/images/spacer.gif" alt="" width="1" height="5" border="0" /></td>
</tr>
<tr>
    <td class="borderhorizontal" colspan="2"><img src="/images/spacer.gif" alt="" width="1" height="1" border="0" /></td>
</tr>
<tr>
    <td class="bgColorContent" colspan="2"><img src="/images/spacer.gif" alt="" width="1" height="5" border="0" /></td>
</tr>
</table>

    </td>
        <td class="bgRight"><img src="/images/spacer.gif" alt="" width="8" height="1" border="0" /></td>
    </tr>
    <tr>
        <td class="bgLeft"><img src="/images/spacer.gif" alt="" width="8" height="1" border="0" /></td>
        <td class="bgColorContent"><span class="sharedCalculatorsClassToHideText">You may be interested in <a href="/content/padvisor_home/keeping_fuel_costs_down.html">viewing our ideas</a> for keeping your fuel costs down.</a></span></td>
        <td class="bgRight"><img src="/images/spacer.gif" alt="" width="8" height="1" border="0" /></td>
    </tr>
    <tr>
        <td class="cornerBottomLeft"><img src="/images/spacer.gif" alt="" width="8" height="8" border="0" /></td>
        <td class="bgBottom"><img src="/images/spacer.gif" alt="" height="1" border="0" /></td>
        <td class="cornerBottomRight"><img src="/images/spacer.gif" alt="" width="8" height="8" border="0" /></td>
    </tr>
</table>

</form>

<br />


</div>


</div>

代替document.form.element嘗試使用document.getElementById('id-of-the-input-element')訪問DOM元素。

驗證您的html以獲取該錯誤列表。 絕對不要將IE用作工作方式的參考。 始終要始終先檢查現代瀏覽器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM