如何防止刷新但还要提交表单呢? [英] How to prevent refresh on submit but also actually submit the form?

查看:65
本文介绍了如何防止刷新但还要提交表单呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多堆栈溢出的帖子,它们告诉用户通过使用return false或将输入类型更改为button来阻止页面刷新,但这可以通过禁用提交来实现,而我需要这样做.如何使用按钮保持提交表单的启用状态,而无需在使用后刷新页面?

I've seen many stack overflow posts telling the user to prevent the page refresh by using return false or by changing the input type to button, but this works by disabling submission, which I need. How do I keep submission of my form enabled using my button, without refreshing the page upon use?

此帖子与假定的重复完全不同.该帖子仅与表单的提交有关,而本文的重点在于提交时无需刷新页面

this post is entirely different from the supposed duplicate. That post is is only concerned with submission of the form, whereas the crux of this post is submission without page refresh

推荐答案

您可以在jquery中使用.submit()函数提交表单.

you can submit a form using the .submit() function in jquery

$('#your-id-form').submit( function(e) { 
   //prevent submit
   e.preventDefault(); //Thx @alex

   //do things on submit
   $.ajax({
       data: "data",
       type: "POST",
       url: "your-destiny.php",
       beforeSend: function(){
        //before send data
       },
       success: function(data){
           // the data
           console.log(data);
       }
   });
 });

干杯

这篇关于如何防止刷新但还要提交表单呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆