简体   繁体   中英

jquery mobile javascript calculator

I am very new to javascript, and I am trying to build a mobile app with Jquery Mobile. I would like the user to input one value on each page and then on the last page they will click submit and it will display the value on the last page. I have been working on this for like 4 hours and I can get the calculation to work on the same page as the submit button but not on the next page.

    <script>

        function sum()
{


    var item1num = document.getElementById('item1num').value;
    var item2num = document.getElementById('item2num').value;

        {result = parseInt(item1num)*parseInt(item2num);

        document.getElementById("showResult").innerHTML = (result);}}

    </script>

</head>
<body>
    <!-- Home -->
    <div data-role="page" id="page1">
        <div data-role="content">
            <div data-role="fieldcontain" id="item1">
                <fieldset data-role="controlgroup">
                    <label for="item1">
                        item1
                    </label>
                    <input name="item1num" id="item1num" placeholder="" value="" type="number" />
                </fieldset>
            </div>
            <a data-role="button" data-transition="slidefade" href="#page2" data-theme="b">
                Next
            </a>
        </div>
    </div>
    <div data-role="page" id="page2">
        <div data-role="content">
            <div data-role="fieldcontain" id="item1">
                <fieldset data-role="controlgroup">
                    <label for="item2">
                        item2
                    </label>
                    <input name="item2num" id="item2num" placeholder="" value="" type="number" />
                </fieldset>
            </div>
            <input id="btnAdd" type="submit" value="Submit" onclick="sum();" data-theme="b" />
            <span id="showResult"></span>
        </div>
    </div>
    <div data-role="page" id="page3">
        <div data-role="content">
            <span id="showResult"></span>
        </div>
    </div>

You need to pass the value to the pages submitting a form

// page1

<form action="page2" method="post">
    <input type="text" name="value1"/>
    <input type="text" name="value2"/>
    <input type="submit" />
</form>

so in the other page you can get both and work with

// page2

<input type="hidden" name="value1" value="value from page 1"/>
<input type="hidden" name="value2" value="other value from page 1"/>

to get this with jQuery you can do

var val1 = jQuery('input[name=value1]');
var val2 = jQuery('input[name=value2]');

Please try below code.It's displaying the result in the final page.

<!DOCTYPE html>
    <html>
    <head>
        <title>Page Title</title> 

        <meta name="viewport" content="width=device-width, initial-scale=1"> 

        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
        <script>
        function sum()
    {

        var item1num = document.getElementById('item1num').value;
        var item2num = document.getElementById('item2num').value;


            {result = parseInt(item1num)*parseInt(item2num);
            document.getElementById("showResult").innerHTML = "Result is: "+(result);}
    }

        </script>
    </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-role="content">
                <div data-role="fieldcontain" id="item1">
                    <fieldset data-role="controlgroup">
                        <label for="item1">
                            item1
                        </label>
                        <input name="item1num" id="item1num" placeholder="" value="" type="number" />
                    </fieldset>
                </div>
                <a data-role="button" data-transition="slidefade" href="#page2" data-theme="b">
                    Next
                </a>
            </div>
        </div>
        <div data-role="page" id="page2">
            <div data-role="content">
                <div data-role="fieldcontain" id="item1">
                    <fieldset data-role="controlgroup">
                        <label for="item2">
                            item2
                        </label>
                        <input name="item2num" id="item2num" placeholder="" value="" type="number" />
                    </fieldset>
                </div>
                <a href="#page3" data-role="button" data-theme="b" data-transirion="slidefade" onclick="sum();">B</a>
            </div>
        </div>
        <div data-role="page" id="page3">
            <div data-role="content">
                <span id="showResult"></span>
            </div>
        </div>
    </body>
    </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM