2015-07-21 159 views
2

以下是我的表單模板,它加載在我的register.html頁面的標題中,並顯示在div#register-tpl中。它顯示正常。用戶會被提示輸入用戶名,電子郵件和密碼進行註冊:使用Parse Javascript SDK註冊用戶

<script id="register-tpl" type="text/x-handlebars-template"> 
    <form class="form-register" role="form"> 
     <h1>Sign up!</h1> 
     <h2><span class="typcn typcn-user-add"></span>&nbsp;Create an account</h2> 
     <p><input type="text" id="username" name="username" class="form-control" placeholder="Username" required="" autofocus=""></p> 
     <p><input type="password" id="password" name="password" class="form-control" placeholder="Password" required=""></p> 
     <p><input type="text" id="email" name="email" class="form-control" placeholder="Email" required="" autofocus=""></p> 
     <p><button class="button" type="submit">Sign up</button></p> 
     <p>Already have an account? <a class="app-link" href="admin.html">Log in!</a></p> 
    </form> 
</script> 

下面是用於添加新用戶對數據庫的我的Javascript代碼:

   var RegisterView = Parse.View.extend({ 
       template: Handlebars.compile($('#register-tpl').html()), 
       events: { 
        'submit .form-register': 'register' 
       }, 
       register: function(e) { 

        e.preventDefault(); 

        var username = document.getElementById('username').value;  
        var password = document.getElementById('password').value; 
        var email = document.getElementById('input').value; 

        var user = new Parse.User(); 
        user.set("username", username); 
        user.set("password", password); 
        user.set("email", email); 

        user.signUp(username, password, email, { 

         success: function(user) { 
          var welcomeView = new WelcomeView({ model: user }); 
          welcomeView.render(); 
          $('.register-container').html(welcomeView.el); 
         }, 

         error: function(user, error) { 
          console.log(error); 
         } 
        }); 
       }, 
       render: function(){ 
        this.$el.html(this.template()); 
       } 
      }); 

沒有人有任何想法爲什麼這不提交所需的信息?它甚至不認可在我的Parse儀表板上創建新用戶。這裏有什麼根本性的缺陷嗎?謝謝,所有幫助非常感謝!

回答

0

解決方案涉及指定要在成功回調中提交的表單:document.getElementById(「user-add」)。submit()。設置字段變量時,我也有一個常見的拼寫錯誤。它應該讀取var username = form.username.value;而不是username = document.username.value;!

  var RegisterView = Parse.View.extend({ 
       template: Handlebars.compile($('#register-tpl').html()), 
       events: { 
        'submit .user-add': 'register' 
       }, 
       register: function(e){ 
        e.preventDefault(); 

        user = new Parse.User(); 

        var form = document.getElementById("user-add"); 

        var username = form.username.value; 
        var password = form.password.value; 
        var email = form.email.value; 

        user.set("username", username); 
        user.set("password", password); 
        user.set("email", email); 

        user.signUp(null, { 
         success: function(user) { 
          form.submit(); 
          window.location.href = 'admin.html'; 
         }, 
         error: function(user, error) { 
          alert("Error: " + error.code + " " + error.message); 
         } 
        }); 
       }, 
       render: function(){ 
        this.$el.html(this.template()); 
       } 
      });