将JavaScript答案计算到文本框中,而不是弹出窗口!

时间:2010-02-03 00:25:45

标签: javascript eval

我在HEAD中有这个代码:

<script LANGUAGE="JavaScript">
<!--
function calculate(form)

        {

                height = eval(form.height.value)
                width = eval(form.width.value)
                photos = eval(form.photos.value)
                lgtext = eval(form.lgtext.value)
                mountlam = eval(form.mount.value)
                mountlam = eval(form.lam.value)
                GetPriceOne (form, height, width, photos, lgtext, mount, lam) 

        }

        function GetPriceOne(form, height, width, photos, lgtext, mount, lam)

        {

                PriceOne = height * width
                GetPriceTwo(form, height, width, photos, lgtext, mount, lam, PriceOne)
        }

        function GetPriceTwo(form, height, width, photos, lgtext, mount, lam, PriceOne)

        {

                PriceTwo = PriceOne / 144
                GetPriceThree(form, height, width, photos, lgtext, mount, lam, PriceTwo)

        }

        function GetPriceThree(form, height, width, photos, lgtext, mount, lam, PriceTwo)

        {

                PriceThree = PriceTwo * 15
                GetPriceFour(form, height, width, photos, lgtext, mount, lam, PriceThree)

        }

        function GetPriceFour(form, height, width, photos, lgtext, mount, lam, PriceThree)

        {

                if(form.lgtext.checked)
                {
                        PriceFour = PriceThree + 20
                        GetPriceFive(form, height, width, photos, lgtext, mount, lam, PriceFour)
                }
                else
                {
                        PriceFour = PriceThree
                        GetPriceFive(form, height, width, photos, lgtext, mount, lam, PriceFour)
                }

        }

        function GetPriceFive(form, height, width, photos, lgtext, mount, lam, PriceFour)

        {

                if(form.mount.checked)
                {
                        PriceFive = PriceFour + PriceTwo * 5
                        GetPriceSix(form, height, width, photos, lgtext, mount, lam, PriceFive)
                }
                else
                {
                        PriceFive = PriceFour
                        GetPriceSix(form, height, width, photos, lgtext, mount, lam, PriceFive)
                }

        }

        function GetPriceSix(form, height, width, photos, lgtext, mount, lam, PriceFive)

        {

                if(form.lam.checked)
                {
                        PriceSix = PriceFive + PriceTwo * 5
                        GetPriceSeven(form, height, width, photos, lgtext, mount, lam, PriceSix)
                }
                else
                {
                        PriceSix = PriceFive
                        GetPriceSeven(form, height, width, photos, lgtext, mount, lam, PriceSix)
                }

        }


        function GetPriceSeven(form, height, width, photos, lgtext, mount, lam, PriceSix)

        {

        total = (photos * 4.95) + PriceSix
        WriteDocument(total)

        }

        function RoundToPennies(n)

        {

        pennies = n * 100;
        pennies = Math.round(pennies);
        strPennies = "" + pennies;
        len = strPennies.length;
        return strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len);

        }

        function WriteDocument(total)

        {

                alert("Estimated price of this collage is ONLY $" + RoundToPennies(total))
        }
//-->
</script>

如果我想进入此文本框,我需要对该功能做些什么?

<INPUT TYPE = Text NAME = "collageEstimate" SIZE = 25 />
<input type="button" value="Calculate Estimate" name="B1" onclick="calculate(this.form)" />

请帮忙!我已经在这几个小时了,并尝试了我所知道的一切!这是现在的实际页面,使用alert-popup:http://procollage.com/site10/pricing/photo-collage-pricing.html

3 个答案:

答案 0 :(得分:3)

这条线试图做什么?

height = eval(form.height.value)

如果您只是想将其作为数字阅读,那么请执行以下操作:

height = parseFloat(form.height.value);

无论如何,将WriteDocument函数更改为:

function WriteDocument(total) {
    document.yourFormName.collageEstimate.value = "Estimated price of this college "
                                           + "is ONLY $" + RoundToPennies(total);
}

您需要让HTML看起来像这样:

<form name="yourFormName">
    <input type="text" name="collageEstimate" size="25" />
    <input type="button" value="Calculate Estimate" name="B1" onclick="calculate(this.form)" />
</form>

答案 1 :(得分:0)

您可以尝试:

document.getElementById('collageEstimate').value = yourvalue

编辑:我发现您有name=collageEstimate,您可以更改为id=collageEstimate

答案 2 :(得分:0)

好的,要在文本输入上显示计算值,您应该获得对它的引用,例如:

function WriteDocument(total) {
  document.forms['myForm'].elements['collageEstimate'].value = RoundToPennies(total);
}

您只需将myForm替换为表单名称即可。

但是除了你的主要问题之外,我对你的代码有几点评论:

  1. 您不需要使用eval将String值转换为Number,您可以只使用一元加运算符,Number构造函数作为函数调用,或{{ 1}}:

    parseFloat
  2. 您应该使用var height = +form.height.value; // or var height = Number(form.height.value); // or var height = parseFloat(form.height.value); 语句声明变量,否则如果在当前范围内找不到它们,它们将变为全局变量。 e.g:

    var
  3. 我还建议您在分配,函数调用和返回语句中使用分号。