简体   繁体   English

使用Javascript提交表单无效

[英]Form submitting using Javascript not working

I am using multiple form in a page here is my HTML code: 我在页面中使用多种形式,这是我的HTML代码:

<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>

<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>


<form name="myform" id="myform" method="post" action="test.php">
<a href="javascript: submitform();" >submit</a>
<input type="hidden" name="encKey" value="12" />
</form>

And here is my javascript code: 这是我的JavaScript代码:

<script type="text/javascript">
function submitform()
{
  document.myform.submit();
}
</script>

When i click on submit link, form is not submitting and gererate error in backend: 当我单击“提交”链接时,表单未提交,并且后端出现gererate错误:

TypeError: document.myform.submit is not a function. TypeError:document.myform.submit不是函数。

What may be issue? 可能是什么问题?

Thanks 谢谢

I think you should use unique ids for all the forms. 我认为您应该为所有表格使用唯一的ID。 Then you can have a function that takes as a parameter the id of the form that you want to submit and then submit it. 然后,您可以使用一个函数,该函数将要提交然后提交的表单的ID作为参数。 So you can have something like 所以你可以像

<script type="text/javascript">
function submitform(formid)
{
  document.getElementById(formid).submit();
}
</script>

You may try this 你可以试试这个

<script type="text/javascript">
function submitform()
{
  document.getElementById('myform').submit();
}
</script>

the id of a form must be unique, try to change the form id to only name. 表单的ID必须是唯一的,请尝试将表单ID更改为仅名称。

you can try to use 你可以尝试使用

document.getElementById('formId').submit()
to submit the information of a form. 提交表格信息。

You can also use document.forms["myForm"].submit(); 您也可以使用document.forms["myForm"].submit(); . In this case "myForm" is the name of each form and it has to be unique as well as id. 在这种情况下, "myForm"是每个表单的名称,并且它必须是唯一的以及ID。

首先,您有3个相同的ID(myform),一个ID必须是唯一的。

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

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