表示未定义函数时出错
Error saying that function is not defined
控制台日志一直说,单击此按钮时未定义函数。我真的不知道为什么。我有另外两个使用相同格式的弹出窗口,当我为每个弹出窗口按下每个按钮时,它们都说功能未定义。该页面没有说明存在任何明显的javascript语法错误。
感谢的帮助
HTML
<div class="popup" id="popup-subscribe">
<div id="close-subscribe" class="close-button"> </div>
<div class="popup-title">Subscribe</div>
<form method="post" name="subscribe" action="">
<span class="label" style="margin-right: 5px">Name: </span><input type="text" placeholder="Name" name="name" /><br>
<span class="label" style="margin-right: 5px">Email: </span><input type="text" placeholder="Email" name="email_subscribe" /><br>
<div class="errors" id="subscribe-errors"></div>
<div onclick="validateSubscribeForm()" class="button-medium">Sign me up!</div>
</form>
</div>
Javascript
function validateSubscribeForm() {
var emailSubscribe =document.forms["subscribe"]["email_subscribe"].value;
var atpos=emailSubscribe.indexOf("@");
var dotpos=emailSubscribe.lastIndexOf(".");
var name = document.forms["subscribe"]["name"].value;
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=emailSubscribe.length)
{
$("#subscribe-errors").html("The email you entered is not valid");
return false;
}
else if (name == null || name == "")
{
$("#subscribe-errors").html("Please enter a name");
}else {
document.subscribe.submit();
}
}
具体来说,控制台日志错误状态为:Uncaught ReferenceError:validateSubscribeForm未定义
您需要将JavaScript函数定义放在HTML:的head
标记中
<head>
<!-- INCLUDE jQuery library first -->
<script type="text/javascript">
function validateSubscribeForm() {
var emailSubscribe = document.forms["subscribe"]["email_subscribe"].value;
var atpos = emailSubscribe.indexOf("@");
var dotpos = emailSubscribe.lastIndexOf(".");
var name = document.forms["subscribe"]["name"].value;
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= emailSubscribe.length) {
$("#subscribe-errors").html("The email you entered is not valid");
return false;
} else if (name == null || name == "") {
$("#subscribe-errors").html("Please enter a name");
} else {
document.subscribe.submit();
}
}
</script>
</head>
<body>
<div class="popup" id="popup-subscribe">
<div id="close-subscribe" class="close-button"></div>
<div class="popup-title">Subscribe</div>
<form method="post" name="subscribe" action=""> <span class="label" style="margin-right: 5px">Name: </span>
<input type="text" placeholder="Name" name="name" />
<br /> <span class="label" style="margin-right: 5px">Email: </span>
<input type="text" placeholder="Email" name="email_subscribe" />
<br />
<div class="errors" id="subscribe-errors"></div>
<div onclick="validateSubscribeForm()" class="button-medium">Sign me up!</div>
</form>
</div>
</body>
您错过了Jquery。包括Jquery库。
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
相关文章:
- 尝试调用AngularJS上的函数时出错
- 得到"未定义不是函数“;使用显示原型图案时出错
- 从函数中的函数返回数组时出错
- 从 java 脚本调用函数时 PHP 中出错
- 使用PHP和Javascript在函数内部传递alphaneumeric值作为参数时出错
- 比较$watch函数内的基元类型时出错
- CakePHP错误:在非对象上调用成员函数get(),使用cakejavascript助手时出错
- 运行函数时出错
- 将未定义的变量传递给函数时出错
- 尝试编写承诺时出错:承诺解析器对象不是函数
- res.render中出错,未定义函数
- 用javascript中的console.log函数重写警报时出错
- 构造函数出错
- '对象不是函数'尝试将页面对象与Protractor一起使用时出错
- 未捕获的类型错误:this.reset不是函数-在ajax调用后清空输入时出错
- 谷歌应用程序脚本:;范围不是函数,它是对象;尝试在单元格中放置注释时出错
- 从.ascx文件调用Javascript函数时出错
- angularjs[ng:areq]参数'fn'不是函数,在控制台中获取字符串时出错
- 巴别塔&节点:传输后出错((0,_express.express)不是函数)
- 使用ajax将数据发布到asmx web服务并使用javascript函数在html页面中编写响应时出错