表单验证后,表单不提交

时间:2014-05-09 03:12:12

标签: javascript jquery ajax forms

所以我的问题是这个。我有一些"验证"在我的电子邮件和复选框上检查它们是否为空。这似乎工作,但在他们填写并检查后,我仍然收到我的警告消息(.error)弹出窗口,表单不提交。

我之前只使用过电子邮件,但需要添加协议复选框。

以下是代码和jsfiddle

感谢您的帮助!

HTML:

<form class="form" action="">
    <div class="wrapper-input email">
        <input id="email" type="text" name="email" placeholder="youremailaddress@example.com" />
        <button class="form-submit submit">Sign-Up</button>
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <div class="wrapper-input">
        <input type="checkbox" id="terms" name="terms" value="Agree"> <span>Click to agree</span>

    </div> </form> <div class="modal">
    <div class="modal-title">
         <h4 class="success">Submission Successful!</h4>

         <h4 class="error">Submission Error!</h4>

        <img class="close" src="img/close-x.png" alt="close" />
    </div>
    <div class="modal-content">
        <p class="success">Sucess</p>
        <p class="error">Error!</p>
    </div> </div>

的javascript:

$(document).ready(function() {
    $(".submit").click(function() {
        var email = $("#email").val();
        var dataString = 'email='+ email;
        var terms = $("input:checkbox#terms").val();

        if (!terms.checked) {
          $('.lk-modal').show();
            $('.success').hide();
            $('.error').show();
        }  

        if (email ==='') {
          $('.lk-modal').show();
              $('.success').hide();
            $('.error').show();
        }

        else {
            $.ajax({
            type: "POST",
            url: "collect.php",
            data: dataString,
            success: function(){
                $('.lk-modal').show();
                $('.success').show();
                $('.error').hide();
            }
      });
    }
return false;
  });
});

2 个答案:

答案 0 :(得分:3)

编辑:请先查看Stefano Dalpiaz的回答,因为他指出了您的错误。


要检查字符串是否为空,您需要做的就是if ( !email )How do you check for an empty string in JavaScript?)。您还可以使用.is(':checked')来确定是否选中了复选框。

这是一个工作小提琴:http://jsfiddle.net/p28am/1/

HTML:

<form class="form" action="">
    <div class="wrapper-input email">
        <input id="email" type="text" name="email" placeholder="youremailaddress@example.com" />
        <button class="form-submit submit">Sign-Up</button>
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <div class="wrapper-input">
        <input type="checkbox" id="terms" name="terms" value="Agree"> <span>Click to agree</span>

    </div>
</form>
<div class="modal">
    <div class="modal-title">
         <h4 class="success">Submission Successful!</h4>

         <h4 class="error">Submission Error!</h4>

        <img class="close" src="img/close-x.png" alt="close" />
    </div>
    <div class="modal-content">
        <p class="success">Sucess</p>
        <p class="error">Error!</p>
    </div>
</div>

JS:

$(document).ready(function () {
    $(".submit").click(function () {
        var email = $("#email").val();
        var dataString = 'email=' + email;
        var terms = $("input:checkbox#terms").is(':checked');

        if (!email || !terms) {
            $('.modal').show();
            $('.success').hide();
            $('.error').show();
        } else {
            $.ajax({
                type: "/echo/json/",
                url: "collect.php",
                data: dataString,
                success: function () {
                    $('.modal').show();
                    $('.success').show();
                    $('.error').hide();
                }
            });
        }
        return false;
    });
});

        $('.close').click(function(e){
            e.preventDefault();
            $('.modal').hide();
        });

答案 1 :(得分:2)

您的代码有一些错误。对于复选框,您正在检查其值的.checked属性,复选框的值是一个字符串。在检查电子邮件之前,我还添加了else语句。没有它,即使您没有选中复选框,也会发送请求。这是更新版本:

$(document).ready(function () {
    $(".submit").click(function () {
        var email = $("#email").val();
        var dataString = 'email=' + email;
        var terms = $("input:checkbox#terms");

        if (!terms.is(":checked")) {
            $('.modal').show();
            $('.success').hide();
            $('.error').show();
        }

        else if (email === '') {
            $('.modal').show();
            $('.success').hide();
            $('.error').show();
        } else {
            $.ajax({
                type: "/echo/json/",
                url: "collect.php",
                data: dataString,
                success: function () {
                    $('.modal').show();
                    $('.success').show();
                    $('.error').hide();
                }
            });
        }
        return false;
    });
});

这是JsFiddle