简体   繁体   English

提交表单后如何防止表单呈现到新页面?

[英]how to prevent form to render to the new page after submit form?

function post(url, fields) {
  //create a <form> element.
  var p = document.createElement('form');
  //construct the form
  p.action = url;
  p.innerHTML = fields;
  p.target = '_self';
  p.method = 'post';
  //append the form to the current page.
  document.body.appendChild(p);
  //submit the form
  p.submit(function (e) {
    e.preventDefault();
  });
}

I also try this way.我也试试这个方法。 But none of them is working.但他们都没有工作。 Whenever the form is submitted, it will render the page.无论何时提交表单,它都会呈现页面。 I need to stop the form render the page after submit.我需要在提交后停止表单呈现页面。 Thanks for any help!谢谢你的帮助! 另一个尝试

try post method rather than submitting form尝试 post 方法而不是提交表单

 function post(url, fields) { $.post(url,{fields: fields} ,function(data, status){ alert("Data: " + data + "\\nStatus: " + status); }); }

 function post(url, fields) { var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.send(JSON.stringify(fields)); }

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

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