如何在问卷上设置提交按钮

时间:2016-02-23 12:43:03

标签: javascript html form-submit onsubmit

我正在制作健康问卷,我遇到了一些问题。主要问题是else部分。有人能告诉我为什么它不起作用并帮助我排序吗?

我的代码是:                作业4          

健康问卷

    

<body>
<form name="form1">
Please enter the following details: <p>
First Name: <br>
<input type="text" name="txtFirstName"> size="20" maxlength="20"> <p>
Surname: <br>
<input type="text" name="txtSurName"> size="30" maxlength="20"> <p>
Age: <br>
<input type="text" name="txtAge" size="3" maxlength="3"> <p>
Address Line 1: <br>
<input type="text" name="txtAddressline1" size="30" maxlength="30"> <p>
Address Line 2: <br>
<input type="text" name="txtAddressline2" size="30" maxlength="30"> <p>
City: <br>
<input type="text" name="txtCity" size="20" maxlength="20"> <p>
County: <br>
<input type="text" name="txtCounty" size="20" maxlength="20"> <p>
Post Code: <br>
<input type="text" name="txtPostCode" size="10" maxlength="10"> <p>

<input type="submit" value="Check Details" name=validateForm
    onclick="validateForm_onclick()">

<script type ="text/javascript">
function validateForm_onclick()
{
var myForm = document.form1;
if(myForm.txtAge.value === "" || myForm.txtFirstName.value === ""|| 

myForm.txtSurName.value === ""|| myForm.txtAddressline1.value === ""|| 
myForm.txtAddressline2.value === ""|| myForm.txtCity.value === ""|| 

myForm.txtCounty.value === ""|| myForm.txtPostCode.value === "")
{
alert("Please complete all of the form");
if(myForm.txtFirstName.value ==="")
{
myForm.txtFirstName.focus();
}
else
{
myForm.txtSurName.focus();
}
else
{
myForm.txtFirstName.focus();
}
else
{
myForm.txtAge.focus();
}
else
{
myForm.txtAddressline1.focus();
}
else
{
myForm.txtAddressline2.focus();
}
else
{
myForm.txtCity.focus();
}
else
{
myForm.txtCounty.focus();
}
else
{
myForm.txtPostCode.focus();
}
}
else
{
alert("Thanks for completing the form " + myForm.txtName.value);
myForm.submit();
}
}
</script
</form>
</body>

我还需要在名称上保留大小,但需要将其隐藏起来。 有人可以给我一些关于如何改变它的建议吗?

2 个答案:

答案 0 :(得分:1)

您的表单没有提交按钮。 你的代码:

<input type="button" value="Check Details" name=validateForm
    onclick="validateForm_onclick()">

整改代码:

<input type="submit" value="Check Details" name=validateForm
        onclick="validateForm_onclick()">

此外,长长的其他列表在JavaScript中是非法的。

答案 1 :(得分:0)

表单名称= form1 需要有引号:表单名称=&#34; form1&#34;

如果您不想使用提交但坚持按钮,请将其插入您的javascript:

else
{
alert("Thanks for completing the form " + myForm.txtName.value);
myForm.submit();
}