0

我对 javascript 很陌生,我正在尝试使用 Jquery Mobile 构建一个移动应用程序。我希望用户在每个页面上输入一个值,然后在最后一页上,他们将单击提交,它将在最后一页上显示该值。我已经为此工作了 4 个小时,我可以让计算在与提交按钮相同的页面上工作,但不能在下一页上工作。

    <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>
4

2 回答 2

0

您需要将值传递给提交表单的页面

// page1

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

所以在另一个页面中,您可以同时获得并使用

// page2

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

用 jQuery 得到这个你可以做

var val1 = jQuery('input[name=value1]');
var val2 = jQuery('input[name=value2]');
于 2013-02-26T02:02:58.133 回答
0

请尝试下面的代码。它在最后一页显示结果。

<!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>
于 2013-02-26T05:08:45.830 回答