代码之家  ›  专栏  ›  技术社区  ›  Packy

从输入jquery获取值

  •  0
  • Packy  · 技术社区  · 11 年前

    我试图从输入中获取值,并将其传递给脚本以注册用户,但没有任何运气存储它。我设置的测试警报显示没有值。以下是我必须获取用户名值并将其传递到脚本中的步骤:

                        <form id ="form-signin" class="form-signin" action="" method="">
                          <input type="text" id="username" class="form-control" placeholder="Username" required autofocus>
                          <input type="email" class="form-control" placeholder="Email" required autofocus>
                          <input type="password" class="form-control" placeholder="Password" required>
                          <input type="submit" id="createUser" class="btn btn-lg btn-default btn-block" value="Sign Up" />
                        </form>
    
    
    
    <script type="text/javascript">
    
        Parse.initialize("u3BTp3Efoko8hbNhl5MCeli8Kd2iiEk8mE4vYgn4", "tQEGymAWeB8Tr2LA3YDGoq2Lt2xpGMW9ikeFSTtD");
    
        //get the input data
        var username = $('#username').val();
    
        //set the user
        var user = new Parse.User();
    
        $( "form" ).submit(function( event ) {
    
          alert( "Error: " + username + " " );
    
            user.set("username", " " + username + " ");
            user.set("password", "my pass");
            user.set("email", "email@example.com");
    
            // other fields can be set just like with Parse.Object
            user.set("phone", "415-392-0202");
    
            user.signUp(null, {
              success: function(user) {
                // Hooray! Let them use the app now.
              },
              error: function(user, error) {
                // Show the error message somewhere and let the user try again.
                alert("Error: " + error.code + " " + error.message);
              }
            });
    
        });
    
    </script>
    
    1 回复  |  直到 11 年前
        1
  •  2
  •   Arun P Johny    11 年前

    您需要在提交处理程序中读取用户名的值。

    您正在读取字段的值 #username 当加载的页面为空时,一旦读取值并将其分配给变量,变量值将不会在更新输入值时更新。

    //dom ready handler
    jQuery(function ($) {
        Parse.initialize("u3BTp3Efoko8hbNhl5MCeli8Kd2iiEk8mE4vYgn4", "tQEGymAWeB8Tr2LA3YDGoq2Lt2xpGMW9ikeFSTtD");
    
        //set the user
        var user = new Parse.User();
    
        $("form").submit(function (event) {
            //get the input data
            var username = $('#username').val();//read the user value in s
    
            alert("Error: " + username + " ");
    
            user.set("username", " " + username + " ");
            user.set("password", "my pass");
            user.set("email", "email@example.com");
    
            // other fields can be set just like with Parse.Object
            user.set("phone", "415-392-0202");
    
            user.signUp(null, {
                success: function (user) {
                    // Hooray! Let them use the app now.
                },
                error: function (user, error) {
                    // Show the error message somewhere and let the user try again.
                    alert("Error: " + error.code + " " + error.message);
                }
            });
    
        });
    })