jquery 验证后提交表单

发布于 2024-12-22 17:17:42 字数 1229 浏览 2 评论 0原文

我有以下表单:

<form id="form" action="comments.php" method="post">
  <ul>
    <li><div class="formbox_titles">Name</div><input type="text" name="name" class="required comm_input" /></li>
    <li><div class="formbox_titles">Email</div><input type="text" name="email" class="required comm_input"/></li>
    <li><div class="formbox_titles">Message</div><textarea name="message" class="required comm_text"></textarea></li>
    <li><input type="submit" value="Submit"></li>
  </ul>
</form>

并遵循用于表单提交的 JQuery:

target: '#preview', 
  success: function() { 
  $('#formbox').slideUp('fast'); 
}

现在的问题是我也有表单验证 JQuery 代码

$().ready(function() {
    // validate comment form
    $("#form").validate({ 
    });
});

它们都工作得很好,并且表单确实弹出警告,所有三个字段不能为空,但表单内的日期已提交到数据库中反正。我使用以下代码进行表单验证 http://bassistance.de/jquery-plugins/jquery -plugin-validation/ 所以问题是如何在第二个里面添加第一个jquery,这样一旦必填字段被填充,第一个就会被执行?

希望有人能帮忙。谢谢。

I have following form:

<form id="form" action="comments.php" method="post">
  <ul>
    <li><div class="formbox_titles">Name</div><input type="text" name="name" class="required comm_input" /></li>
    <li><div class="formbox_titles">Email</div><input type="text" name="email" class="required comm_input"/></li>
    <li><div class="formbox_titles">Message</div><textarea name="message" class="required comm_text"></textarea></li>
    <li><input type="submit" value="Submit"></li>
  </ul>
</form>

and following JQuery for form submission:

target: '#preview', 
  success: function() { 
  $('#formbox').slideUp('fast'); 
}

Now the problem is that I also have form validation JQuery code

$().ready(function() {
    // validate comment form
    $("#form").validate({ 
    });
});

Both of them work great and form does popup warning that all three fields cant be empty but the date inside form is submitted into database anyway. Im using following code for form validation http://bassistance.de/jquery-plugins/jquery-plugin-validation/ So the question is how to add first jquery inside second, so first will be executed once the required fields are filled?

Hope someone will help. Thanks.

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

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

发布评论

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

评论(5

油饼 2024-12-29 17:17:42

如果你想手动提交表单,你必须绕过 jQuery 绑定事件。您可以通过以下任一方式执行此操作:

$('#form_id')[0].submit();

或者

$('#form_id').get(0).submit();

[0] 或 get(0) 为您提供 DOM 对象而不是 jQuery 对象。

If you want to submit the form manually, you have to bypass jQuery bound event. You can do it in either of these ways:

$('#form_id')[0].submit();

or

$('#form_id').get(0).submit();

[0] or get(0) gives you the DOM object instead of the jQuery object.

悲凉≈ 2024-12-29 17:17:42

从您的示例中,我不清楚页面的控制流,但是,我假设您在某处调用 submit() 方法。验证表单后并在提交之前,请使用 valid() 方法检查其验证情况。您的代码应该如下所示:

$("#form").validate();
if ($('#form').valid())
    $('#form').submit();

From your example, I am not clear about the control flow of your page, however, I am assuming you are calling the submit() method somewhere. After you have validated the form and before you submit it, check it's validation with the valid() method. Your code should look something like this:

$("#form").validate();
if ($('#form').valid())
    $('#form').submit();
完美的未来在梦里 2024-12-29 17:17:42

我对您的验证插件一无所知,但通常您可以使用这种和平的代码

<script>

    $("form").submit(function() {
      if (validationIsTrue()) {
        return true;
      }
      else {
        return false;
      }
    });
</script>

您必须进行验证,然后将 true / false 返回到表单的提交功能。如果插件确实返回 bool 值,您可以尝试如下操作:

$("form").submit(function() {
   return $(this).validate({ .... });
});

I do not know anything about your validation plugin, but normally u could use this peace of code

<script>

    $("form").submit(function() {
      if (validationIsTrue()) {
        return true;
      }
      else {
        return false;
      }
    });
</script>

You have to make your validation and then return true / false to the submit function of the form. If the plugin does return a bool value, you can try something like this:

$("form").submit(function() {
   return $(this).validate({ .... });
});
压抑⊿情绪 2024-12-29 17:17:42

当您说“提交表单内的日期”时,这对我来说毫无意义,因为在您的代码片段中我看不到任何称为“日期”的内容。

我广泛使用了 .validate() 插件......
我可以告诉你的是,你正在使用的验证插件有一个可以使用的submitHandler函数,这可能会有所帮助...

// validate comment form
$("#form").validate({
submitHandler : function(form) {
    //do something here
    form.submit();
}
});

仅当满足所有验证规则时才会调用submitHandler。也就是说,在您的情况下,您有三个带有“必填”的字段,这意味着除非所有三个字段都有值,否则不会提交此表单。

这样,我可以肯定地告诉您,根据所提供的信息,没有任何信息表明您所描述的问题。提供更多更好的信息将帮助我们找出问题所在。

When you say that the "date inside form is submitted" that makes no sense to me because in your code snippets I see nothing called "date".

I've used the .validate() plugin extensively...
What I can tell you that may help is that the validate plugin you're using has a submitHandler function that you can use...

// validate comment form
$("#form").validate({
submitHandler : function(form) {
    //do something here
    form.submit();
}
});

The submitHandler is called only if all the rules for validation are met. Namely in your case you have three fields with "required" on them which means that this form will NOT be submitted unless all three fields have a value.

With that, I can tell you for sure that with the information provided there is nothing to indicate the problem you're describing. Providing more and better information will help us figure out what's wrong.

剪不断理还乱 2024-12-29 17:17:42

您可以使用 JQuery 表单验证插件中的 SubmitHandler

<script type="text/javascript" src="resources/jquery.min.js" ></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script type='text/javascript'>
$(document).ready(function() {
    $(".form").validate({
        rules: {
            userName: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            userName: {
                required: "specify userName"
            },
            password: {
                required: "specify password"
            }
        },
        errorClass: "help-inline text-danger",
        errorElement: "span",
        highlight: function(element, errorClass, validClass) {
            $(element).parents('.form-group').addClass('has-error');
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).parents('.form-group').removeClass('has-error');
            $(element).parents('.form-group').addClass('has-success');
        },
        submitHandler: function(form,e) {
            e.preventDefault();
            console.log('Form submitted');
            $.ajax({
                type: 'POST',
                url: 'authenticate.jsp',
                dataType: "html",
                data: $('form').serialize(),
                success: function(result) {
                    window.location.href = "dashboard.jsp";
                },
                error : function(error) {

                }
            });
            return false;
        }
    });

});  
</script>

You can use submitHandler from JQuery form validate plugin

<script type="text/javascript" src="resources/jquery.min.js" ></script> 
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script type='text/javascript'>
$(document).ready(function() {
    $(".form").validate({
        rules: {
            userName: {
                required: true
            },
            password: {
                required: true
            }
        },
        messages: {
            userName: {
                required: "specify userName"
            },
            password: {
                required: "specify password"
            }
        },
        errorClass: "help-inline text-danger",
        errorElement: "span",
        highlight: function(element, errorClass, validClass) {
            $(element).parents('.form-group').addClass('has-error');
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).parents('.form-group').removeClass('has-error');
            $(element).parents('.form-group').addClass('has-success');
        },
        submitHandler: function(form,e) {
            e.preventDefault();
            console.log('Form submitted');
            $.ajax({
                type: 'POST',
                url: 'authenticate.jsp',
                dataType: "html",
                data: $('form').serialize(),
                success: function(result) {
                    window.location.href = "dashboard.jsp";
                },
                error : function(error) {

                }
            });
            return false;
        }
    });

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