在我的html文件的头部定义了脚本但是仍然不起作用是因为我的表单的动作导致了php文件?
<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["food_name"].value;
if (x==null || x=="")
{
alert("Food name must be filled out");
return false;
}
</script>
<title> my title is going here </title>
</head>
<body>
<form name="myForm" action="http://xxxxxxxxx/~xxxxxxxx/file/phpfile.php" method="post" onsubmit="validateForm()">
<b>foodName:</b> <input type="text" name="food_name"><br>
<b>Food Type:</b> <input type="text" name="food_type"><br>
<b>Total:</b> <input type="text" name="total"><br>
<b>Available:</b> <input type="text" name="available"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
答案 0 :(得分:1)
您的validateForm
功能
答案 1 :(得分:1)
修复validateForm函数缺少的大括号}
并更新onsubmit to this
onsubmit="return validateForm()"
另外,即使验证失败,它也会提交表格