Jquery 验证表单未在“返回 true”时提交

发布于 2024-10-20 03:02:42 字数 2900 浏览 0 评论 0原文

哇,我的第一篇 Stackoverflow 帖子,我已经关注这个网站很多年了..无论如何,所以我有一个表单,我正在使用 Jquery 进行一些简单的验证,并且出于某种原因,即使“返回 true”正在通过,我通过在它之前放置一个警报来测试它。这是我的 jquery,它没有出现任何错误:

            var captcha_def = 'Please copy the text';

        $('input[type="submit"]').click( function(x){
            x.preventDefault();
            var validated;

            $('.validate').each(function(){
                var content = $(this).val();
                if ( content == '' )
                {
                    $(this).addClass('error');
                    validated = 'false';
                }
                else
                {$(this).removeClass('error');}
            });


            $('#conf_email').each( function(){  //Just to check to make sure their is a Email Confirmation

                var email = $("#email").val();
                var conf_email = $('#conf_email').val();
                if (email != conf_email)
                {
                    $('#email, #conf_email').addClass('error');
                    validated = 'false';
                }
                else
                {
                    $('#email, #conf_email').removeClass('error');
                }
            });



                var captcha = $('#captcha_input').val();
                if (captcha == captcha_def)
                {
                    $('#captcha_input').addClass('error');
                    validated = 'false';
                }
            alert(validated);

            if (validated === 'false') 
            {
                return false;
                } 
            else
            {   
                return true;
            }

        });

这是我的 html(已验证):

<form id='freeform' name='a' method="post" action="index.php"  >


    <div id="contact_form">
        <h2><span class="small-caps">Send</span> US AN <span class="small-caps">Email</span></h2>

        <label for="name_input" class="text">*Your Name:</label><input type="text" name="name" class="text validate" id="name_input" /><br />
        <label for="email_input" class="text">*Email:</label><input type="text" name="email" class="text validate email" id="email_input" /><br />
        <label for="ta_message" class="textarea">Your Message:</label><br />
        <textarea id="ta_message" name="message"></textarea><br />

        <label for="captcha_input" class="text"><img src="/images/captchas" width="140" height="30" style="border:0;" alt=" " /></label><input type="text" name="captcha" class="captcha text validate" id="captcha_input" /><br />

        <input type="submit" id="submit" value="Submit"> 
    </div>

我一直在为这件事绞尽脑汁,不明白这是什么,随着最后期限的临近,我试图弄清楚我搞砸了什么。非常感谢您的帮助!

Wow, my first Stackoverflow post, I've been watching this site for ages .. anyway, so Ive got a form, that I'm using Jquery to do some simple validation, and for some reason, even though the 'return true' is coming through, I tested it by putting an alert just before it. Heres my jquery, which isnt coming up with any errors:

            var captcha_def = 'Please copy the text';

        $('input[type="submit"]').click( function(x){
            x.preventDefault();
            var validated;

            $('.validate').each(function(){
                var content = $(this).val();
                if ( content == '' )
                {
                    $(this).addClass('error');
                    validated = 'false';
                }
                else
                {$(this).removeClass('error');}
            });


            $('#conf_email').each( function(){  //Just to check to make sure their is a Email Confirmation

                var email = $("#email").val();
                var conf_email = $('#conf_email').val();
                if (email != conf_email)
                {
                    $('#email, #conf_email').addClass('error');
                    validated = 'false';
                }
                else
                {
                    $('#email, #conf_email').removeClass('error');
                }
            });



                var captcha = $('#captcha_input').val();
                if (captcha == captcha_def)
                {
                    $('#captcha_input').addClass('error');
                    validated = 'false';
                }
            alert(validated);

            if (validated === 'false') 
            {
                return false;
                } 
            else
            {   
                return true;
            }

        });

and heres my html (which validates):

<form id='freeform' name='a' method="post" action="index.php"  >


    <div id="contact_form">
        <h2><span class="small-caps">Send</span> US AN <span class="small-caps">Email</span></h2>

        <label for="name_input" class="text">*Your Name:</label><input type="text" name="name" class="text validate" id="name_input" /><br />
        <label for="email_input" class="text">*Email:</label><input type="text" name="email" class="text validate email" id="email_input" /><br />
        <label for="ta_message" class="textarea">Your Message:</label><br />
        <textarea id="ta_message" name="message"></textarea><br />

        <label for="captcha_input" class="text"><img src="/images/captchas" width="140" height="30" style="border:0;" alt=" " /></label><input type="text" name="captcha" class="captcha text validate" id="captcha_input" /><br />

        <input type="submit" id="submit" value="Submit"> 
    </div>

I've been banging my head over this, and cant figure out what it is, and with a deadline looming, I'm trying to figure out what I messed up. Thanks you so much for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动听の歌 2024-10-27 03:02:42

为什么你一开始就调用 x.preventDefault() 而不进行任何验证?仅当验证失败时才阻止默认行为怎么样?

Why are you calling x.preventDefault() at the beginning without doing any validation at all? What about prevent the default behavior only when your validation fails?

恋你朝朝暮暮 2024-10-27 03:02:42

x.preventDefault();行阻止控件(在本例中为提交)发生默认行为。删除该行或在 return false 之前移动,它应该可以工作。

The x.preventDefault(); line prevent the default behaviour of the control (in this case, the submit) from happening. Remove that line or move if right before the return false and it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文