简体   繁体   English

防止从DOM表单提交刷新

[英]Prevent refreshing from DOM Form Submit

I was wondering if it was possible to prevent the page from refreshing on submission with the code I have below: 我想知道是否可以通过以下代码阻止提交时刷新页面:

var new_element         = document.createElement('input');
new_element.type        = 'hidden';
new_element.name        = 'save';
new_element.value       = '';

var form_element = document.getElementById('document_form');    

form_element.appendChild(new_element);

form_element.submit();  

The form submit action is a navigation event, if you don't want a page reload you have to not use the submit action. 表单提交动作是一个导航事件,如果您不希望重新加载页面,则不必使用提交动作。 Then you need to projcet another type of strategy, for example using ajax to send a request and obtain a response. 然后,您需要提出另一种策略,例如使用ajax发送请求并获得响应。

this is a jquery example: 这是一个jQuery示例:

$.post( "ajax/test.html", function( data ) {
  //data contains the response
});

or using AngularJS as you wrote in your comment: 或使用您在评论中写的AngularJS:

$http({
  method: 'POST',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

here details about agularjs $http object https://docs.angularjs.org/api/ng/service/$http 这里有关agularjs $ http对象https://docs.angularjs.org/api/ng/service/$http的详细信息

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

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