2013-10-08 13 views
0

提交時,我正在製作一個簡單的表單,用戶在其電子郵件地址旁邊提交視頻。我想這樣做是爲了讓個人在填寫電子郵件並保存視頻之前不能提交表單。即使email.value ==「」

視頻部分正在工作,但是當我用空電子郵件測試時,它似乎仍然提交。代碼如下,現場版本是http://www.atlas-china.com/record-your-multi-lingual-abilties/

幫助非常感謝!

// Global variable to hold player's reference. 
var _Nimbb; 

// Global variable to hold the guid of the recorded video. 
var _Guid = ""; 

// Event: Nimbb Player has been initialized and is ready. 
function Nimbb_initCompleted(idPlayer) { 
    // Get a reference to the player since it was successfully created. 
    _Nimbb = document[idPlayer]; 
} 

// Event: the video was saved. 
function Nimbb_videoSaved(idPlayer) { 
    _Guid = _Nimbb.getGuid(); 
} 

jQuery(document).ready(function ($) { 

    // Get the data from the form. Check that everything is completed. 
    $('#video_submit').click(function (e) { 

     var email = document.getElementById("email").value; 
     var video_title = document.getElementById("video_title").value; 
     var form = document.myForm; 

     // Make sure the email is specified. 
     if (email.value == "") { 
      alert("Please enter your email to proceed."); 
      return; 
     } 

     // Verify that the video is not currently recording. 
     if (_Nimbb.getState() == "recording") { 
      alert("The video is being recorded. Please wait."); 
      return; 
     } 

     // Check that video has been recorded. 
     if (_Guid == "") { 
      alert("You did not save the video. Click save."); 
      return; 
     } 

     // Set the guid as hidden parameter. 
     form.guid.value = _Guid; 

     var dataString = 'email=' + email + '&guid=' + _Guid + '&video_title=' + video_title; 
     //alert (dataString);return false; 
     $.ajax({ 
      type: "POST", 
      url: "<?php echo get_template_directory_uri();?>/send.php", 
      data: dataString, 
      success: function() { 
       $('#contact_form').html("<div id='message'></div>"); 
       $('#message').html("<h2>Contact Form Submitted!</h2>") 
        .append("<p>We will be in touch soon.</p>") 
        .hide() 
      } 
     }); 
     document.forms["myForm"].submit(); 
    }); 
}); 
+1

不管這種檢查的結果有下面這行前,您必須驗證服務器上的結果。 – asawyer

+1

如果您返回false,該怎麼辦? – jonhopkins

+1

你爲什麼要做ajax post *和*提交表單? – Pointy

回答

0

查看正在運行的網站後...

email = "" // Empty string 

email.value = undefined 

試着改變你的代碼

if (email === "") { 
    alert("Please enter your email to proceed."); 
    return; 
} 

我假設你會被誤做.value的兩倍,你在你的代碼

var email = document.getElementById("email").value; 
0

當您從該處理函數返回時,瀏覽器將繼續執行「submit」事件的正常行爲,即提交表單。

你可以改變你的 「中止」 return語句來

return false;