Form not submitted with JS

I have the simplest javascript function in the world:

fnSubmit()
{
  window.print();
  document.formname.submit();
}

which is called:

<button type="button" id="submit" onclick="fnSubmit()">Submit</button>

Everything is fine and good, the print dialog box appears, however, after printing or canceling printing, I get the following error:

"document.formname.submit is not a function"

My form is defined as follows: (obviously, I am not using the form name in the actual code, but you get the idea)

<form name="formname" id="formname" method="post" action="<?=$_SERVER['SCRIPT_NAME']?>">

Obviously, I'm not trying to do something special here, and I used similar approaches in the past, what am I missing here in the world?

+5
source share
5 answers

: id - , "". , .

, . , document.formname.submit - , . document.formname.submit , DOM node, .

, DOM node name id. , :

<form name="example" id="example" action="/">
  <input type="text" name="exampleField" />
  <button type="button" name="submit" onclick="document.example.submit(); return false;">Submit</button>
</form>

document.forms.example.exampleField DOM node, "exampleField". JS , : document.forms.example.exampleField.value.

, "submit", , document.forms.example.submit. , , .

EDIT:

, . JavaScript:

function hack() {
  var form = document.createElement("form");
  var myForm = document.example;
  form.submit.apply(myForm);
}

. HTML- JavaScript?

+27

, id, name, :

form id:

document.getElementById('formname').submit();

form tag name:

document.forms['formname'].submit();
+6

:

fnSubmit()
{
  window.print();
  document.getElementById("formname").submit();
}
+5

The most likely culprit is IE, which confuses JavaScript variables, identifiers, and names. Find in your source something that shares the name of your form.

0
source
  • Place the enter button inside your form.
  • Give tabindex = "- 1" on it.
  • Make this invisible using style = "display: none;".

Like this

<input type="submit" tabindex="-1" style="display:none;" />
0
source

All Articles