Chrome扩展表单提交

时间:2015-01-29 15:22:13

标签: javascript forms google-chrome-extension

我想要做的是创建一个调用函数,该函数可以创建一个表单并在每次调用特定页面时提交它..e.g

function foo(url){
  var form = document.createElement("form");
  var input1 = document.createElement("input");
  var input2 = document.createElement("input");

  form.method = "POST";
  form.action = url;
  form.name = "myForm";
  form.id = "form_id";

  input1.type = "hidden";
  input1.value = "value";
  input1.blahblah...

  input2.type = "hidden";
  input2.value = "value";
  input2.blahblah...

  form.appendChild(input1);
  form.appendChild(input2);
  document.body.appendChild(form);

  form.submit();

  alert("Form submitted");
}

问题是,即使表单附加在文档中,提交也不会因某种原因而起作用。 (警报永远不会出现);

问题出在哪里?

(或任何替代方案)

由于

1 个答案:

答案 0 :(得分:0)

function foo(url)
{
  var form1 = document.createElement("form");
  var input1 = document.createElement("input");
  var input2 = document.createElement("input");

  form1.method = "POST";
  form1.action = url;
  form1.name = "myForm";
  form1.id = "form_id";

  input1.type = "hidden";
  input1.value = "value";

  input2.type = "hidden";
  input2.value = "value";

  form1.appendChild(input1);
  form1.appendChild(input2);
  document.body.appendChild(form1);

  form1.submit();

  alert("Form submitted");
}