-3

我尝试使用此代码计算并返回一个邮件表单,但不是计算,它在我添加邮件表单之前执行,返回 Uncaught ReferenceError: calculate is not defined 代码如下:

<form>
    <div class="main">
        <div class="gap">
            <table>
                <tr>
                    <td width="400">Current GPH:</td>
                    <td>
                        <input type="text" id="current_gph" onKeyUp="calculate();" class="dark" value="0" />
                    </td>
                </tr>
                <tr>
                    <td width="400">GPH Savings:</td>
                    <td>
                        <input type="text" id="gph_savings" onKeyUp="calculate();" class="dark" value="10" />&#37;</td>
                </tr>
                <tr>
                    <td>Number of Hours Per Day:</td>
                    <td>
                        <input type="text" id="hours_per_day" onKeyUp="calculate();" class="dark" value="0" />
                    </td>
                </tr>
                <tr height="50">
                    <td>Gallons of Fuel Used Per Month:</td>
                    <td>
                        <div id="gallons"></div>
                    </td>
                </tr>
                <tr>
                    <td>Fuel Cost Per Gallon:</td>
                    <td>
                        <input type="text" id="fuel_cost_per_gallon" onKeyUp="calculate();" class="dark" value="0" />
                    </td>
                </tr>
                <tr>
                    <td>Number of Vehicles In Fleet:</td>
                    <td>
                        <input type="text" id="number_of_vehicles" onKeyUp="calculate();" class="dark" value="0" />
                    </td>
                </tr>
                <tr height="50">
                    <td>Cost of Fuel:</td>
                    <td>
                        <div id="cost"></div>
                    </td>
                </tr>
                <tr>
                    <td>Investment per Vehicle:</td>
                    <td>
                        <input type="text" id="investment_per_vehicle" onKeyUp="calculate();" class="dark" value="0" />
                    </td>
                </tr>
                <tr>
                    <td>Investment for the Fleet:</td>
                    <td>
                        <div id="investment_for_the_fleet"></div>
                    </td>
                </tr>
            </table>
        </div>
        <div class="report" id="report">
            <div id="calc-container">
                <div class="paper">
                    <div class="tape"></div>
                    <h2>Please Input Your GPH Details Above To See What ERT Can Do For You.</h2>
                </div>
                <!--end paper-->
            </div>
            <!--end calc-container-->
        </div>
        <br />
        <br />
        <br />
        <br />
        <br />
        <br />
        <input type="text" value="Name" id="email_name" />
        <input type="button" value="Reset" onclick="resetcalculator();" />
    </div>
    <script>
        var addcomma = function(x) {
            x = String(x);
            return x.replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
        }
        var seperate = function(x) {
            return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        }
        var calculate = function() {
            var current_gph = document.getElementById('current_gph').value;
            var gph_savings = 1 - (document.getElementById('gph_savings').value / 100);
            var result_gph_savings = document.getElementById('gph_savings');
            var hours_per_day = document.getElementById('hours_per_day').value;
            var fuel_cost_per_gallon = document.getElementById('fuel_cost_per_gallon').value;
            var number_of_vehicles = document.getElementById('number_of_vehicles').value;
            var investment_per_vehicle = document.getElementById('investment_per_vehicle').value;
            var email_name = document.getElementById('email_name').value;
            var monthNames = ["January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December"];
            var email_date_raw = new Date();
            var email_date = monthNames[email_date_raw.getMonth()] + " "
            email_date_raw.getDate() + ", "
            email_date_raw.getFullYear();
            current_gph = parseFloat(current_gph);
            hours_per_day = parseFloat(hours_per_day);
            fuel_cost_per_gallon = parseFloat(fuel_cost_per_gallon);
            var gallons = hours_per_day * 30 * current_gph;
            var gallons2 = Math.round(gallons);
            if (isNaN(gallons) || gallons == "Infinity" || gallons == "-Infinity") {
                document.getElementById('gallons').innerHTML = "Invalid Input";
            } else {
                document.getElementById("gallons").innerHTML = seperate(gallons2);
            }
            var investment_for_the_fleet = investment_per_vehicle * number_of_vehicles;
            investment_for_the_fleet = investment_for_the_fleet.toFixed(2);
            if (isNaN(investment_for_the_fleet) || gallons == "investment_for_the_fleet " || gallons == "-investment_for_the_fleet ") {
                document.getElementById('investment_for_the_fleet').innerHTML = "Invalid Input";
            } else {
                document.getElementById("investment_for_the_fleet").innerHTML = "$" + addcomma(investment_for_the_fleet);
            }

            var cost = number_of_vehicles * fuel_cost_per_gallon * gallons;
            cost = cost.toFixed(2);
            if (isNaN(cost) || cost == "Infinity" || cost == "-Infinity") {
                document.getElementById('cost').innerHTML = "Invalid Input";
            } else {
                document.getElementById("cost").innerHTML = "$" + addcomma(cost);
            }
            var result_gph = gph_savings * current_gph;
            var new_gallons = hours_per_day * 30 * result_gph;
            var result_new_gallons_day = (new_gallons / 30) * number_of_vehicles;
            var result_new_fuel_cost = fuel_cost_per_gallon * new_gallons * number_of_vehicles;
            var result_monthly_savings = cost - result_new_fuel_cost;
            var result_annual_savings = result_monthly_savings * 12;
            var result_fuel_saved = (gallons - new_gallons) * number_of_vehicles;
            var result_annual_carbon_removed = result_fuel_saved / 100;
            var result_carbon_credit = result_annual_carbon_removed * 20;
            if (isNaN(result_gph) || result_gph == "Infinity" || result_gph == "-Infinity" || isNaN(result_new_fuel_cost) || result_new_fuel_cost == "Infinity" || result_new_fuel_cost == "-Infinity" || isNaN(investment_per_vehicle) || isNaN(result_monthly_savings) || result_monthly_savings == "Infinity" || result_monthly_savings == "-Infinity") {
                document.getElementById('report').innerHTML = '<div id="calc-container"><div class="paper"><div class="tape"></div><h1>Invalid Input</h1></div><!--end paper--></div><!--end calc-container-->';
            } else {
                document.getElementById('report').innerHTML = '<div id="calc-container"><div class="paper"><div class="tape"></div><h1>Result:</h1>We Guarantee A <span id="result_gph_savings"></span>% Savings In GPH<br /><br />Your NEW GPH: <span id="result_gph"></span><br />Your NEW Fuel Consumption Per Day: <span id="result_new_gallons_day"></span><br />Your NEW Gallons Used Per Month:  <span id="result_new_gallons"></span><br />Your Current Fuel Cost Is $<span id="cost1"></span> Monthly.<br />With ERT, Your NEW Fuel Cost Is $<span id="result_new_fuel_cost"></span> Monthly.<br /><br />Monthly Savings: $<span id="result_monthly_savings"></span><br />Annual Savings: $<span id="result_annual_savings"></span><br />Annual Gallons of Fuel Saved: <span id="result_fuel_saved"></span><br />Annual Tonnes of Carbon Removed From The Air: <span id="result_annual_carbon_removed"></span><br />Total Value of Carbon Credits: $<span id="result_carbon_credit"></span></div><!--end paper--></div><!--end calc-container-->';
                result_gph = result_gph.toFixed(2);
                document.getElementById('result_gph').innerHTML = addcomma(result_gph);
                result_new_fuel_cost = result_new_fuel_cost.toFixed(2);
                document.getElementById('result_new_fuel_cost').innerHTML = addcomma(result_new_fuel_cost);
                document.getElementById('cost1').innerHTML = addcomma(cost);
                result_monthly_savings = result_monthly_savings.toFixed(2);
                document.getElementById('result_monthly_savings').innerHTML = addcomma(result_monthly_savings);
                result_annual_savings = result_annual_savings.toFixed(2);
                document.getElementById('result_annual_savings').innerHTML = addcomma(result_annual_savings);
                result_annual_carbon_removed = result_annual_carbon_removed.toFixed(2);
                document.getElementById('result_annual_carbon_removed').innerHTML = addcomma(result_annual_carbon_removed);
                result_fuel_saved = Math.round(result_fuel_saved);
                document.getElementById('result_fuel_saved').innerHTML = seperate(result_fuel_saved);
                result_carbon_credit = result_carbon_credit.toFixed(2);
                document.getElementById('result_carbon_credit').innerHTML = addcomma(result_carbon_credit);
                new_gallons = new_gallons * number_of_vehicles;
                new_gallons = Math.round(new_gallons);
                document.getElementById("result_new_gallons").innerHTML = seperate(new_gallons);
                result_new_gallons_day = Math.round(result_new_gallons_day);
                document.getElementById("result_new_gallons_day").innerHTML = seperate(result_new_gallons_day);
                document.getElementById("mail_body").innerHTML = "Dear " + email_name + ",\nOn " + email_date + "\nYour Provided Data\nCurrent GPH: " + current_gph + "\nNumber of Hours Per Day: " + hours_per_day + "\nFuel Cost Per Gallon: $" + fuel_cost_per_gallon + "\nNumber of Vehicles In Fleet: " + number_of_vehicles + "\nInvestment per Vehicle: $" + investment_per_vehicle + "/n/nERT GPH Saving Result";
            }
        }
        document.getElementById('current_gph').value = 0;
        document.getElementById('hours_per_day').value = 0;
        document.getElementById('fuel_cost_per_gallon').value = 0;
        document.getElementById('number_of_vehicles').value = 1;
        document.getElementById('investment_per_vehicle').value = 0;
        document.getElementById('gph_savings').value = 10;
        var resetcalculator = function() {
            document.getElementById('current_gph').value = 0;
            document.getElementById('hours_per_day').value = 0;
            document.getElementById('fuel_cost_per_gallon').value = 0;
            document.getElementById('number_of_vehicles').value = 1;
            document.getElementById('investment_per_vehicle').value = 0;
            document.getElementById('gph_savings').value = 10;
            document.getElementById('report').innerHTML = '<div id="calc-container"><div class="paper"><div class="tape"></div><h2>Please Input Your GPH Details Above To See What ERT Can Do For You.</h2></div><!--end paper--></div><!--end calc-container-->';
        }
    </script>
    <textarea name="mail_body" id="mail_body"></textarea>
</form>
4

1 回答 1

1

您在该行中有一个语法错误,您缺少一些+连接符号

var email_date = monthNames[email_date_raw.getMonth()] + " " email_date_raw.getDate() + ", " email_date_raw.getFullYear();

修复它就像

var email_date = monthNames[email_date_raw.getMonth()] + " " +email_date_raw.getDate() + ", "+ email_date_raw.getFullYear();
于 2013-05-22T12:53:28.683 回答