2014-07-24 91 views
-1

我創建了一個登錄頁面,我有兩個單獨的表單來提交電子郵件地址,我正在使用Parse來存儲它們。一次提交兩個表單

問題我在第一張表格上提交了一個電子郵件地址,它創建了兩個對象 - 一個帶有表單中的電子郵件地址,另一個是空白的。當我以第二種形式提交電子郵件地址時,它會按預期提交一個對象和一個電子郵件地址。

從這裏我只能得出結論,當我提交第一個表單時,第二個表單將被同時提交。

下面是我在控制檯中看到:

Subscribe           Parse.js:39 

                Parse.js:48 

Notify Me           Parse.js:7 

[email protected]          Parse.js:16 

New object created with objectId: FodANtALX8  Parse.js:21 

New object created with objectId: hLYyCNVSaW  Parse.js:53 

下面的代碼:

$(document).ready(function() { 

$(".input-group-btn").click(function() { 
    console.log("Notify Me"); 

    var Address = $(".form-control").val(); 

    var Email = Parse.Object.extend("Email"); 
    var email = new Email(); 

    email.set("Address", Address); 

    console.log(Address); 

    email.save(null, { 
     success: function(email) { 
      // Execute any logic that should take place after the object is saved. 
      console.log('New object created with objectId: ' + email.id); 

     }, 
     error: function(email, error) { 
      // Execute any logic that should take place if the save fails. 
      // error is a Parse.Error with an error code and description. 
      alert('Could not accept email address: ' + error.message); 
     } 
    }); 

}); 


$(".btn-wide").click(function() { 
    console.log("Buy"); 
}); 

$(".btn-primary").click(function() { 
    console.log("Subscribe"); 

    var Address = $(".subinput").val(); 

    var Email = Parse.Object.extend("Email"); 
    var email = new Email(); 

    email.set("Address", Address); 

    console.log(Address); 

    email.save(null, { 
     success: function(email) { 
      // Execute any logic that should take place after the object is saved. 
      console.log('New object created with objectId: ' + email.id); 
      $('.subinput').val(""); 
     }, 
     error: function(email, error) { 
      // Execute any logic that should take place if the save fails. 
      // error is a Parse.Error with an error code and description. 
      alert('Could not accept email address: ' + error.message); 
     } 
    }); 

}); 

}); 

,並且是HTML的相應部分:

<form> 
     <div class="input-group"> 
      <input type="text" class="form-control" placeholder="[email protected]"> 
      <span class="input-group-btn"> 
      <button class="btn btn-primary" type="button">Notify Me</button> 
      </span> 
     </div> 
</form> 


<form> 
    <div class="col-sm-8"> 
     <input type="text" class="subinput" placeholder="Enter your email for updates" spellcheck="false"> 
     </div> 
     <div class="col-sm-4"> 
      <button class="btn btn-large btn-primary" type="submit"> 
       Subscribe now 
      </button> 
     </div> 
</form> 
+3

您可以提供您的HTML,可能您的兩個表單元素共享一個類,並且當單擊一個按鈕時,您的事件處理程序會查找兩個表單。解決這個問題的一個好方法是將唯一的ID與您的事件相關聯,而不是類。 – bencripps

+0

@bencripps添加了HTML。 – Chad

+0

由於您用於綁定的類,您的'click'處理程序都會觸發。改爲使用「ID」按鈕。或者更好的是,追蹤每個「表單」上的'submit'事件,而不是按鈕上的'click'事件。 – Mathletics

回答

1

在你Subscribe功能你問jquery尋找btn-primary這是一個你共享的css類r按鈕。添加一個唯一的ID到你的按鈕,並讓jquery尋找這個以避免這個問題重演。