Submitting multiple forms with Javascript (no refresh)
I am trying to run this in Chrome/Firefox Console but not having any luck.
(function(d) {
var form = d.getElementsByTagName('form');
var i = form.length;
while (i--) {
form[i].addEventListener("submit", function(evt){
evt.preventDefault();
开发者_Go百科 }, false);
xhr = new XMLHttpRequest();
xhr.open("POST", "http://example.com/", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("media_id=" + form[i].elements["media_id"].value);
}
})(this.document);
Can anyone spot any problems with it? Thanks
EDIT: I have fixed the problem. Thanks @Niklas
The problem is that you are preventing the form submissions but not actually doing anything to send them. You'd presumably want to add some ajax to send the data.
精彩评论