开发者

Have JavaScript answer calculate into a textbox, not a pop-up!

I have this code in the 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, 开发者_JS百科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>

If I want it to go into this text box, what do i need to do to the function?

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

Please help! I've been at this for hours and have tried everything I know!!! Here is the actual page as it now, working with the alert-popup: http://procollage.com/site10/pricing/photo-collage-pricing.html


What is this line trying to do?

height = eval(form.height.value)

If you're just trying to read it as a number, then do that:

height = parseFloat(form.height.value);

Anyway, change the WriteDocument function to this:

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

You'll need to have your HTML looking something like this:

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


Ok, to show the calculated value on the text input, you should get a reference to it, for example:

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

You just need to replace myForm with the name of your form.

But I have a couple of comments on your code, aside your main question:

  1. You don't need to use eval to convert a String value to Number, you can just use the unary plus operator, the Number constructor called as a function, or parseFloat:

    var height = +form.height.value; // or
    var height = Number(form.height.value); // or
    var height = parseFloat(form.height.value);
    
  2. You should declare your variables with the var statement, otherwise if they aren't found in the current scope, they will become global variables. e.g.:

    //...
    var total = (photos * 4.95) + PriceSix;
    
  3. I would also recommend you to use semicolons on assignments, function calls, and return statements.


You can try :

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

EDIT : I see you have name=collageEstimate, you can change to id=collageEstimate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜