Home > Web Front-end > JS Tutorial > body text

Error causes and solutions when form.submit() cannot submit the form_javascript skills

WBOY
Release: 2016-05-16 16:34:10
Original
1940 people have browsed it

Go directly to the code:

<div id="register">

<h4>会员注册</h4>

<div class="formdiv">

<form method="post" action="register.php&#63;action=register" name="register" id="registerForm">

<dl>

<dt>请认真填写以下内容</dt>

<dd class="hasspan">

用户名:<input type="text" name="username">

</dd>

<dd class="hasspan">

密码:<input type="password" name="pwd">

</dd>

<dd class="hasspan">

确认密码:<input type="password" name="pwdagain">

</dd>

<dd id="tximg">

<img src="img/face/m01.gif" alt="选择头像" id="faceimg">

<input type="hidden" name="touxiang" value="">

<label id="imgsrclabel">m01.gif</label>

</dd>

<dd style="margin-right:120px;">

验证码:<input type="text" name="code">

<span>

<img src="code.php" alt="code" id="code" name="code">

<a href="#code" id="change">换一张</a>

</span>

</dd>

<dd class="btns">

<input type="button" name="submit" id="submit" value="注册">

<input type="button" name="quit" id="quit" value="退出">

</dd>

</dl>

</form>

</div>

</div>

表单数据提交到本页面,下面是js处理

/*注册表单提交*/

function formDeal()

{

var btnSubmit = document.getElementById('submit');

var formId = document.getElementById('registerForm');

btnSubmit.onclick = function()

{

//表单的submit()方法不能提交表单

formId.submit();

}


}

如果表单提交,在本页面有一段提示信息

if(!empty($_GET['action']) && $_GET['action'] == 'register')

{

echo '你提交了数据';

exit();

}
Copy after login

The result is that after testing for a long time, I did not see the prompt message. I thought that the code was wrong or the method was written wrong. I checked carefully and checked the official results document to confirm that there was no error.

formId.submit() cannot be submitted, so I have to change the type of btnSubmit to submit

this.type="submit"

After checking the information online, the reasons can be summed up in two points:

1. The form cannot have the tag name="submit"

2. "enctype="multipart/form-data"" cannot be missing from the form

After testing, these two points are ridiculous and do not solve my problem (maybe my problem environment is different)

Later, I thought some forum friends suggested that I change the ID of the registration button to a new name instead of submitting. After the correction, the form is submitted normally and the prompt message appears.

Final summary: Do not set the id of the button to submit, otherwise it may cause confusion and cause the submit() method of the form to fail to submit the form. When naming the ID, it is best not to duplicate the name with the existing API to avoid unnecessary annoyance.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!