简体   繁体   English

使用JavaScript提交表单后如何停止打开ACTION页面

[英]How to stop opening ACTION page after submitting FORM using JavaScript

When I am submitting the FORM using SUBMIT button, it takes me to the ACTION page. 当我使用“提交”按钮提交表单时,它将带我进入“操作”页面。 But I want to stay in same page after submission and show a message below the form that "Submitted Successfully", and also reset the form data. 但是我想在提交后留在同一页面上,并在“成功提交”表单下方显示一条消息,并重置表单数据。 My code is here... 我的代码在这里...

<h1>CONTACT US</h1>
<div class="form">
<form action="https://docs.google.com/forms/d/e/1FAIpQLSe0dybzANfQIB58cSkso1mvWqKx2CeDtCl7T_x063U031r6DA/formResponse" method="post" id="mG61Hd">
Name
<input type="text" id="name" name="entry.1826425548">

Email
<input type="text" id="email" name="entry.2007423902">

Contact Number
<input type="text" id="phone" name="entry.1184586857">

Issue Type
<select id="issue" name="entry.1960470932">
  <option>Feedback</option>
  <option>Complain</option>
  <option>Enquiry</option>
</select>
Message
<textarea name="entry.608344518"></textarea>

<input type="submit" value="Submit" onclick="submit();">
</form>
<p id="form_status"></p>
</div>

You need to use Ajax for sending Data and no refresh the page. 您需要使用Ajax发送数据并且不刷新页面。

//Jquery for not submit a form.
$("form").submit( function(e) {
    e.preventDefault();
     return false;

});

//Ajax Example
$.ajax({
        data: {yourDataKey: 'yourDataValue', moreKey: 'moreLabel'},
        type: "POST", //OR GET
        url: yourUrl.php,   //The same form's action URL
        beforeSend: function(){
            //Callback beforeSend Data          
        },
        success: function(data){                
            //Callback on success returning Data                
        }
    });

Instead of adding onclick="submit()" to your button try capturing the submit event. 与其在您的按钮上添加onclick =“ submit()”,不如尝试捕获Submit事件。 In your submit function you also need to return false and prevent default to prevent the form from not submitting and taking you to the action page. 在您的Submit函数中,您还需要返回false并阻止default来防止表单不提交并将您带到操作页面。

Using jQuery it would look something like this: 使用jQuery看起来像这样:

$("#id_form").on("submit", function(e){
   //send data through ajax
   e.preventDefault();
   return false;
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM