无法动态更改表单操作属性

时间:2012-02-14 15:43:28

标签: javascript jquery jquery-mobile

这是我的代码:

<!DOCTYPE html>
<html>

    <head>

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="gm.css" />
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile.structure-1.0.min.css"/>
        <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>

        <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
        <script src="assets/custom.js"></script>
        <script type="text/javascript" src="assets/ejs_production.js"></script>
    </head>

    <body>
        <div data-role="page" data-add-back-btn="true" id="pageregister">
            <div data-role="header">

                <h1>Sign Up</h1>
            </div>
            <!-- /header -->
            <div data-role="content">

        <form action="someurl.html" method="post" id="registerform">

                <fieldset>
                    <div data-role="fieldcontain">
                        <label for="email">Email:</label>

                        <input type="text" name="email" id="email" value="" />
                    </div>
                    <div data-role="fieldcontain">
                        <label for="email2">Re-type email:</label>
                        <input type="text" name="email2" id="email2" value=""
                        />
                    </div>
                    <div data-role="fieldcontain">
                        <label for="password">Password:</label>

                        <input type="password" name="password" id="password"
                        value="" />
                    </div>
                    <div data-role="fieldcontain">
                        <label for="password2">Re-type password:</label>
                        <input type="password" name="password2" id="password2"
                        value="" />
                    </div>



                    <button type="submit"
                    data-theme="b" name="submit" value="submit-value">Sign Up</button>
                </fieldset>

        </form>



        <script>


        $('#pageregister').live('pageshow', function (event) {

                    var url = baseURL + "users/add.json";
            $("registerform").attr("action", url); //THIS HAS NO EFFECT?????
            alert(url); //I SEE THIS

                });


        </script>


            </div>
            <!-- /content -->
        <div data-role="footer" data-position="fixed">
    <div data-role="navbar">

        <ul>
            <li>
                <a href="game_search.html" data-icon="search">Search</a>
            </li>
            <li>

                <a href="info.html" data-icon="info">Info</a>
            </li>
        </ul>
    </div>
    <!-- /navbar -->
</div>
<!-- /footer -->
        </div>
        <!-- /page -->

    </body>

</html>

1 个答案:

答案 0 :(得分:1)

您的选择器错误,您在ID选择器中缺少#

更改此

$("registerform").attr("action", url);

$("#registerform").attr("action", url);

作为旁注,请始终将您的代码包装在DOM ready事件中,以确保在尝试查找元素之前DOM可用。

$(function(){
   //Your code goes here

   $('#pageregister').live('pageshow', function (event) {
        var url = baseURL + "users/add.json";
        $("#registerform").attr("action", url);
   });
});