Javascript 未捕获的 ReferenceError:未定义验证(jQuery 验证)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27036837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught ReferenceError: validate is not defined (jQuery validation)
提问by Hussein Shehady
I am getting this error and the script never seems to work.
我收到此错误,脚本似乎永远无法工作。
Uncaught ReferenceError: validate is not defined
未捕获的 ReferenceError:未定义验证
I also tried to call the function onsubmit from the and also tried to place the function inside the page and in a separate JS file but same results.
The html file:show.gsp
我还尝试从 调用 onsubmit 函数,并尝试将该函数放置在页面内和单独的 JS 文件中,但结果相同。html文件:show.gsp
<body>
<div class="row">
<div class="col-md-9 col-sm-9">
<g:form class="form-inline" role="form" action="update" name="form" >
<div class="form-group has-feedback">
<label class="control-label" for="mcc">GSM.Identity.MCC</label>
<br/>
<input type="text" class="form-control required" name="mcc" id="mcc" value="${mcc}">
<span class="glyphicon form-control-feedback"></span>
</div>
<br/>
<br/>
<div class="form-group has-feedback">
<label class="control-label" for="mnc">GSM.Identity.MNC</label>
<br/>
<input type="text" class="form-control required" name="mnc" id="mnc" value="${mnc}">
<span class="glyphicon form-control-feedback"></span>
</div>
<br/>
<br/>
<div class="form-group has-feedback">
<label class="control-label" for="co">GSM.Radio.CO</label>
<br/>
<input type="text" class="form-control required" name="co" id="co" value="${co}">
<span class="glyphicon form-control-feedback"></span>
</div>
<br/>
<br/>
<p>
<button type="submit" class="btn btn-success btn-sm" id="submit" onclick="validate()">Save Settings</button>
<g:link action="restart" class="btn btn-danger btn-sm">Restart OpenBts</g:link>
</p>
</g:form>
</div>
</div>
</body>
<script src="${resource(dir:'js', file:'main_script.js') }"></script>
and the script: main_script.js
和脚本: main_script.js
//validation script
function validate(){
console.log("test");
var mcc = $("#mcc").val();
var mnc = $("#mnc").val();
var co = $("#co").val();
if (!$.isNumeric(mcc)){
$("#mcc").parent("div").removeClass("has-feedback has-error has-success").addClass("has-error");
}
if (!$.isNumeric(mnc)){
$("#mnc").parent("div").removeClass("has-feedback has-error has-success").addClass("has-error");
}
if (!$.isNumeric(co)){
$("#co").parent("div").removeClass("has-feedback has-error has-success").addClass("has-error");
}
}
回答by Straw Hat
Javascript must be loaded within <head>or <body>tags.
必须在<head>或<body>标签内加载 Javascript 。
you have added script tag after </body>tag, thats why it is not getting loaded and button event can't find validate()function which is within that Javascript
您在标签后添加了脚本标签</body>,这就是为什么它没有被加载并且按钮事件找不到validate()该 Javascript 中的函数
Add it before </body>tag as follows,
将其添加到</body>标签前,如下所示,
.
.
.
</g:form>
</div>
</div>
<script src="${resource(dir:'js', file:'main_script.js') }"></script>
</body>
As This Linksays it should be used within <body>tag
正如此链接所说,它应该在<body>标签内使用

