![](/img/trans.png)
[英]Hey i have a problem when I press the button I have an error in the console log which says my function is not defined
[英]Javascript error log says function not defined when I have defined it in a separate file and even the same file
这是我能想到的最小的最简单的开始,当然会出错。 我正在运行.cshtml页面。 这是在SAME文件中具有功能的代码,仅相隔几行:
@section head
{
<script>
function CheckPostalCode(form) {
if (form.postalcode.value.length < 5) {
alert("Please enter a Zip Code.");
form.postalcode.focus();
return false;
}
return (true);
}
</script>
}
现在在下面的网页中:
<form onsubmit="return CheckPostalCode(this)" method="post" action="/search/index.cshtml">
<input type="text" name="postalcode" id="postalcode" size="30" maxlength="5" required/>
<input type="submit" name="submit" value="go" />
</form>
我尝试使用的原始方法是拥有一个名为checkFields.js的separte文件,并将该文件包含在页面部分的标题中,如下所示:
<script type="text/javascript" src="/js/checkFields.js"></script>
网页代码相同的地方。 任何人都可以帮助解释为什么即使脚本在同一文件中时脚本也未定义。 但实际上我希望能够从另一个文件中调用该函数。 我看过很多教程都说,只要包含它们,就可以调用像normal这样的函数。 好吧,我以为我做到了。
查看页面源代码时,它只是上面带有标题标签的表单
如果您使用@section head从视图中包含一些原始javascript,则需要在_Layout.cshtml文件中使用RenderSection(“ head”,false)(“ head”是您自己定义的名称,您可以将其更改为其他名称名称),在<head></head>
部分中,例如:
<head>
//your other css link or js link here
@RenderSection("head", false)
</head>
如果使用链接,请尝试使用“〜”
<script src="~/js/checkFields.js" type="text/javascript"></script>
或使用:
<script src='@Url.Content("~/js/checkFields.js")' type="text/javascript"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.