即使email.value ==“”提交火灾

时间:2013-10-08 13:25:08

标签: javascript

我正在制作一个简单的表单,用户提交视频以及他们的电子邮件地址。我想这样做,以便在填写电子邮件并保存视频之前,此人无法提交表单。

视频部分正在运行,但是当我使用空电子邮件进行测试时,它似乎仍然可以提交。代码如下,实时版本为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();
    });
});

2 个答案:

答案 0 :(得分:0)

当您从该处理程序函数返回时,浏览器将继续执行“提交”事件的正常行为,即提交表单。

您可以将“abort”返回语句更改为

return false;

答案 1 :(得分:0)

查看您的实际网站后...

email = "" // Empty string

email.value = undefined 

尝试将代码更改为

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

我假设您错误地执行了两次 .value ,因为您的代码前面有以下行

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