简体   繁体   English

停止从DOM刷新提交

[英]Stop submit from refreshing from the DOM

Say I have a form like so on a page: 假设我在页面上有这样的表格:

<form action="/add" method="post">
    <input name="first">
    <button type="submit">Submit</button>
</form>

The problem is when I click on the submit button the page tries to load http://localhost/add as a URL. 问题是,当我单击“提交”按钮时,页面尝试将http://localhost/add为URL。 All I want to do is simply make the HTTP post request from the form, and then not reload the page, is this even possible to do with HTML semantics, or will I have to use some javascript to prevent the default action somehow and then make the request? 我要做的只是从表单中发出HTTP发布请求,然后不重新加载页面,这甚至可能与HTML语义有关,还是我必须使用一些javascript来以某种方式阻止默认操作,然后使请求?

I know there have been many question on the topic, but they all seem to be making the requests via a library or javascript, and not utilising the built in form methods as I am? 我知道有关该主题的问题很多,但它们似乎都是通过库或JavaScript发出请求的,而不是像我一样利用内置的表单方法?

You have to use AJAX to achieve this effect: 您必须使用AJAX才能达到此效果:

The first thing you need to do is prevent the behavior your are talking about: 您需要做的第一件事是防止您正在谈论的行为:

$("button[type='submit']").click(function(e){

    e.preventDefault(); 
    e.stopPropagation();

});

OK, so that question also uses that library you are likely referring to... that is called jQuery -- It will make your life much easier, but if you want to avoid it, then give your button an id, and do this: 好的,所以这个问题也使用了您可能会引用的那个库...叫做jQuery -它会使您的生活更加轻松,但是如果要避免使用它,请给按钮一个id,然后执行以下操作:

document.getElementById("submitButton").onclick = function(e) {
    e.preventDefault();
    e.stopPropagation();
}

Then, you have to make the ajax call --- which involves gathering the form data which you want to send... 然后,您必须进行ajax调用---涉及收集要发送的表单数据...

data = {
first:    document.getElementByName("first").value;

}

and making the actual AJAX (aka XHR) request --- 并提出实际的AJAX(aka XHR)请求-

Which looks like this: 看起来像这样:

var request = new XMLHttpRequest();
request.open('POST', '/my/url', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send(data);

In jQuery, it is a bit simpler : 在jQuery中,这有点简单:

$.ajax({
  type: 'POST',
  url: '/my/url',
  data: { first : $("input[name='first']").value() }
});

Have the server respond with HTTP status code 204 NO CONTENT . 让服务器以HTTP状态代码204 NO CONTENT响应。 This prevents the browser from navigating. 这样可以防止浏览器导航。

Demo: http://jsfiddle.net/9r2e1n74/ 演示: http//jsfiddle.net/9r2e1n74/

(note: this fiddle contains no code of my own; it just points the form in the question to a service that returns the 204 response) (注意:此小提琴不包含我自己的代码;它只是将问题中的表格指向返回204响应的服务)


Alternatively, set a target attribute on the form to have it load the response in some hidden frame. 或者,在form上设置target属性,使其在某个隐藏的框架中加载响应。

Demo: http://jsfiddle.net/p571jy78/ 演示: http//jsfiddle.net/p571jy78/

<form action="/add" method="post" target="formtarget">
    <input name="first">
    <button type="submit">Submit</button>
</form>
<iframe name="formtarget" style="display:none;"></iframe>

Alternatively, use JavaScript to handle the form submission with XMLHttpRequest . 或者,使用JavaScript通过XMLHttpRequest处理表单提交。

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

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