【问题标题】:How to submit form using AJAX without having to leave the page HTML Formsubmit如何使用 AJAX 提交表单而无需离开页面 HTML Formsubmit
【发布时间】:2021-07-25 14:16:18
【问题描述】:

我正在使用 Formsubmit 将联系我们表单发送到我的电子邮件,到目前为止它正在工作。

但我想当用户发送表单时它不会将其重定向到源thank you page,而是我想在同一页面上显示感谢文本。我想用我自己的。

所以我关注了documentation,我想出了以下内容: 不幸的是,当我尝试验证表单按钮时它不起作用

<form id="contactForm" action="https://formsubmit.co/el/myemail" method="POST">
<input type="text"   name="name">
<input type="phone"  name="phone" inputmode="tel">
<input type="hidden" name="_subject" value="Partenaires">
<input type="email"  name="email" >
<input type="text"   name="message" >
<button class="btn btn-primary border rounded-0 shadow-lg d-lg-flex justify-content-lg-center" 
type="submit" name="sendData">Send</button>

<!-- <input type="hidden" name="_next" value="thanks.html">-->

<script type="text/javascript">

var frm = $('#contactForm');
var msg_res ='';

frm.submit(function (e) {

    e.preventDefault();

   
    $.ajax({
        type: frm.attr('method'),
        method: "POST",
        url: "https://formsubmit.co/el/vupawa",
        dataType: 'html',
        accepts: 'application/json',
        data: frm.serialize(),
        success: function (response) {
        $("#message").html(response);
            if(response != msg_res){
                msg_res = response; //store new response
                alert('New message received');
              }
        },
        error: function (response) {
            console.log('An error occurred.');
            console.log(response);
        }
        complete: function(response){
         setTimeout(ajax,1000);
        }
    });
});
});
</script>

注意:取消注释这一行 &lt;input type="hidden" name="_next" value="thanks.html"&gt; 将验证表单并通过另一个页面,这不是我想要的。

有什么想法吗?

【问题讨论】:

  • 从表单中移除 action 属性
  • 在成功函数中添加提醒功能
  • 没用我相信action属性是必需的,当我在本地尝试时我确实得到了警报成功但是当我在主机服务器上尝试时我更新了

标签: javascript html ajax form-submit


【解决方案1】:

为了防止提交你应该在

中返回false
frm.submit(function (e) { 
   return false;
})

或在表单标签中 &lt;form onSubmit="return false"&gt;

var frm = $('#contactForm');
var msg_res ='';

frm.submit(function (e) {

    e.preventDefault();

   
    $.ajax({
        type: frm.attr('method'),
        method: "POST",
        url: "https://formsubmit.co/el/vupawa",
        dataType: 'html',
        accepts: 'application/json',
        data: frm.serialize(),
        success: function (response) {
        $("#message").html(response);
            if(response != msg_res){
                msg_res = response; //store new response
                alert('New message received');
              }
        },
        error: function (data) {
            console.log('An error occurred.');
            console.log(data);
        }
    });
    return false; // here a change
}); 
<form id="contactForm" action="https://formsubmit.co/el/myemail" method="POST">
<input type="text"   name="name">
<input type="phone"  name="phone" inputmode="tel">
<input type="hidden" name="_subject" value="Partenaires">
<input type="email"  name="email" >
<input type="text"   name="message" >
<button class="btn btn-primary border rounded-0 shadow-lg d-lg-flex justify-content-lg-center" 
type="submit" name="sendData">Send</button>

<!-- <input type="hidden" name="_next" value="thanks.html">-->
 

【讨论】:

  • 你不认为e.preventDefault(); 已经在提交处理程序中完成了这项工作吗?
  • 它适用于按钮事件,我们希望防止表单提交
  • 它绝对适用于任何具有默认操作的事件,它不仅限于“按钮事件”。
  • 他已经尝试过了,但在他的情况下不起作用,所以击球手尝试我们所有的选项
  • 是的,但是以其他方式做同样的事情不会有帮助,返回false只会让jQuery在内部做event.preventDefault(),它不会改变任何东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 2017-03-17
  • 1970-01-01
  • 2012-07-26
相关资源
最近更新 更多