使用JavaScript创建表单

Fri*_*end 1 html javascript

这是我的代码,

          var mapForm = document.createElement("form");
          mapForm.target = "_blank";    
          mapForm.method = "POST";
          mapForm.action = "delete";

          // Create an input
          var mapInput = document.createElement("input");
          mapInput.type = "hidden";
          mapInput.name = "uploaded";
          mapInput.value = file.name;

          // Add the input to the form
          mapForm.appendChild(mapInput);

          // Add the form to dom
          document.body.appendChild(mapForm);

          // Just submit
          mapForm.submit();
Run Code Online (Sandbox Code Playgroud)

它确实有效,但在提交值后,它会在一个新窗口中打开动作URL,因为我已经给出了mapForm.target = "_blank";,是否可以在不打开任何窗口的情况下提交表单我的意思是它应该保留在同一个窗口但它不应该去"删除页面"?,而不是使用ajax ...

ale*_*exP 8

您可以将数据发送到隐藏的iframe:

var mapForm = document.createElement("form");
mapForm.target = "myIframe";
mapForm.method = "POST";
mapForm.action = "delete";

//Create an iframe
var iframe = document.createElement("iframe");
iframe.src = "response.php";
iframe.name = "myIframe";
iframe.style.width = '1px';
iframe.style.height = '1px';
iframe.style.visibility = 'hidden';
iframe.style.position = 'absolute';
iframe.style.right = '0';
iframe.style.bottom = '0';
mapForm.appendChild(iframe);

// Create an input
var mapInput = document.createElement("input");
mapInput.type = "hidden";
mapInput.name = "uploaded";
mapInput.value = file.name;

// Add the input to the form
mapForm.appendChild(mapInput);

// Add the form to dom
document.body.appendChild(mapForm);

// Just submit
mapForm.submit();

// Remove mapForm 
document.body.removeChild(mapForm);
Run Code Online (Sandbox Code Playgroud)