我正在创建一个简单的HTML表单并通过onsubmit事件处理程序调用JavaScript文件。奇怪的是,每当我点击提交按钮时,我的JS文件都不会触发。救命?
**更新的代码
这就是我对压缩的HTML文件所拥有的:
<html>
<form name="form01" id="form01" action="http://itins3.madisoncollege.edu/echo.php"
method="post" onsubmit="return checkAllTextBoxes();">
<label for="actualFirstName" class="setWidth">First Name:</label>
<input type="text" name="actualFirstName" id="actualFirstName" />
<input type="submit" value="Send Form" />
</form>
</html>
<script src="/javaScriptFiles/newArtist.js" type="text/javascript"></script>
这就是我的JS文件:
function checkAllTextBoxes()
{
if (document.form01.actualFirstName.value.length < 2)
{
alert("First name is too short- must be at least two characters or more.");
return false;
}
return true;
}
我一直试图弄清楚出了什么问题,但似乎无法在我的代码中找到错误。尝试了JSHint,Firebug(FireFox),甚至HTML在线验证器,没有出现错误。另一副编码眼睛将是一个很大的帮助。谢谢。
答案 0 :(得分:1)
<form name="form01" id="form01" method="post">
<label for="actualFirstName" class="setWidth">First Name:</label>
<input type="text" name="actualFirstName" id="actualFirstName" />
<input type="submit" value="Send Form" onClick="return checkAllTextBoxes();" />
</form>
<Script>
function checkAllTextBoxes()
{
if (document.form01.actualFirstName.value.length < 2)
{
alert("First name is too short- must be at least two characters or more.");
return false;
}
else{
document.form01.action = "http://itins3.madisoncollege.edu/echo.php";
document.form01.submit;
return true;
}
</script>
答案 1 :(得分:0)
您必须将您的js文件链接到您的html。在结束标记
之后添加html页面底部的链接这是我尝试过的,它有效..
function checkAllTextBoxes()
{
if (document.form01.actualFirstName.value.length < 2)
{
alert("First name is too short- must be at least two characters or more.");
return false;
}
return true;
}
<html>
<form name="form01" id="form01" action="http://itins3.madisoncollege.edu/echo.php"
method="post" onsubmit="return checkAllTextBoxes();">
<label for="actualFirstName" class="setWidth">First Name:</label>
<input type="text" name="actualFirstName" id="actualFirstName" />
<input type="submit" value="Send Form" />
</form>
</html>
<script src="myscripts.js"></script>