Ask JavaScript to calculate in the text box, not the popup!

I have this code in 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>

If I want it to fall into this text box, what do I need to do for this function?

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

Please, help! I was at this for hours and tried everything I know! Here is the real page, as it is now, working with a pop-up alert: http://procollage.com/site10/pricing/photo-collage-pricing.html

+3
source share
3 answers

What is this line trying to do?

height = eval(form.height.value)

If you are just trying to read it as a number, do the following:

height = parseFloat(form.height.value);

In any case, change the WriteDocument function to this:

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

You need your HTML to look 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>
+3
source

You can try:

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

EDIT: , name=collageEstimate, id=collageEstimate.

0

, , , :

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

myForm .

, :

  • eval String Number, , Number, , parseFloat:

    var height = +form.height.value; // or
    var height = Number(form.height.value); // or
    var height = parseFloat(form.height.value);
    
  • var, , , global. :.

    //...
    var total = (photos * 4.95) + PriceSix;
    
  • , .

0

Source: https://habr.com/ru/post/1731115/


All Articles