简体   繁体   English

表单验证后,表单不提交

[英]After form validation, form doesn't submit

So my issue is this. 所以我的问题是这个。 I have some "validation" on my email and checkbox to check whether they are empty. 我的电子邮件和复选框上有一些“验证”,以检查它们是否为空。 That seemed to work, but after they have been filled in and checked I still get my warning message (.error) popup and the form does not submit. 这似乎可行,但是在填写并检查它们之后,我仍然收到警告消息(.error)弹出窗口,并且该表单未提交。

I had this working previously with just the email, but needed to add the checkbox for an agreement. 我以前仅使用电子邮件进行此操作,但需要添加协议复选框。

Here is the code and a jsfiddle . 这是代码和jsfiddle

Thank you for any help! 感谢您的任何帮助!

html: 的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: 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;
  });
});

Edit: Please look at Stefano Dalpiaz answer first as he points out your mistakes. 编辑:请先看看Stefano Dalpiaz的答案,因为他指出了您的错误。


All you have to do to check if a string is empty is this if ( !email ) ( How do you check for an empty string in JavaScript? ). 检查字符串是否为空所需要做的就是if ( !email )如何在JavaScript中检查空字符串? )。 You can also use .is(':checked') to determine if a checkbox is checked. 您也可以使用.is(':checked')来确定是否选中了一个复选框。

Here is a working fiddle: http://jsfiddle.net/p28am/1/ 这是一个有效的小提琴: http : //jsfiddle.net/p28am/1/

HTML: 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: 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();
        });

There are a few errors on your code. 您的代码中有一些错误。 For the checkbox, you are checking the .checked property of its value, and the value of a checkbox is a string. 对于复选框,您正在检查其值的.checked属性,并且复选框的值是一个字符串。 I have also added an else statement before checking for the email. 在检查电子邮件之前,我还添加了else语句。 Without it, the request would be sent even if you didn't check the checkbox. 没有它,即使您没有选中该复选框,请求也会被发送。 Here is the updated version: 这是更新的版本:

$(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;
    });
});

And here is the JsFiddle . 这是JsFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM