jquery提交表单无需重新加载页面(JQuery submit form without reloading page)

编程入门 行业动态 更新时间:2024-10-28 20:23:34
jquery提交表单无需重新加载页面(JQuery submit form without reloading page)

我对Jquery不是很熟悉。 我正在尝试制作一个在后台提交的表单,无需重新加载页面。 我有一个隐藏的div,显示并隐藏点击,里面有一个窗体。

我有两个问题:

1)表单验证失败时,表单仍提交。 我试图把验证和提交代码的条件if(validation == valid) { $.ajax.... }但它不能正常工作。

2)表单提交后,div自动隐藏,所以无法看到成功的消息。

代码如下:

$().ready(function() { // validate the form when it is submitted, using validation plugin. var validator = $("#contactform").validate({ errorContainer: container, errorLabelContainer: $(), onkeyup: false, onclick: false, onfocusout: false, errorPlacement: function (error, element) { error.insertBefore(element); } }); }); $(function() { //this submits a form $('input[type=submit]').click(function() { $.ajax({ type: "POST", url: "contact.php", data: $("#contactform").serialize(), beforeSend: function() { $('#result').html('<img src="loading.gif" />'); }, success: function(data) { $('#result').html(data); } }) }) }) //this shows and hides div onlcick $(document).ready(function(){ $(".slidingDiv").hide(); $(".show_hide").show(); $('.show_hide').click(function(){ $(".slidingDiv").slideToggle(); }); });

I'm not very familiar with JQuery. I'm trying to make a form that is submitted in the background without reloading page.

I have a hidden div which shows and hides on click, inside the div there's a form.

I have two problems:

1) When the form validation fails, the form is still submitted. I tried to put validation and submission codes in condition if(validation == valid) { $.ajax.... } but it does not work properly.

2) After the form is submitted the div automatically hides, so successful message cannot be seen.

Here's the code:

$().ready(function() { // validate the form when it is submitted, using validation plugin. var validator = $("#contactform").validate({ errorContainer: container, errorLabelContainer: $(), onkeyup: false, onclick: false, onfocusout: false, errorPlacement: function (error, element) { error.insertBefore(element); } }); }); $(function() { //this submits a form $('input[type=submit]').click(function() { $.ajax({ type: "POST", url: "contact.php", data: $("#contactform").serialize(), beforeSend: function() { $('#result').html('<img src="loading.gif" />'); }, success: function(data) { $('#result').html(data); } }) }) }) //this shows and hides div onclick $(document).ready(function(){ $(".slidingDiv").hide(); $(".show_hide").show(); $('.show_hide').click(function(){ $(".slidingDiv").slideToggle(); }); });

最满意答案

而不是绑定到点击事件( input[type=submit] ),你应该绑定到表单的submit事件。

$("form").on("submit", function (e) { e.preventDefault();

我也不确定验证。 如果它异步运行,则需要回调。 如果它同步运行,什么时候触发? 它似乎应该在提交表单时完成,除非您的验证插件自行完成:

$("form").on("submit", function (e) { e.preventDefault(); if ($(this).validate(options)) { $.ajax

使用e.preventDefault将阻止重新加载,并应允许显示您的成功div。

Rather than bind to the click event (input[type=submit]) you should bind to the submit event for the form.

$("form").on("submit", function (e) { e.preventDefault();

I'm also not sure about the validation. If it runs asynchronously, a callback is required. If it runs synchronously, when does it get triggered? It seems like it should be done when the form is submitted unless your validation plugin does that on its own:

$("form").on("submit", function (e) { e.preventDefault(); if ($(this).validate(options)) { $.ajax

Using e.preventDefault will prevent reload and should allow your success div to display.

更多推荐

function,div,form,电脑培训,计算机培训,IT培训"/> <meta name="description

本文发布于:2023-08-01 11:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357784.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   加载   页面   jquery   JQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!