在来到这里之前,我尝试了 stackoverflow 中列出的几个链接,但它们都没有帮助我解决此问题,或者可能是我做错了什么。
我的aspx中有一个输入按钮,并且有serverclick事件以及onclick,客户端和服务器方法都被正确调用,我只在“onclick'函数位于外部文件中(位于同一目录中)。
我什至引用了aspx中的js文件。
注意:
all other controls are working in external js except this.
inline function works fine, but not when moved in external function.
I even tried window.onload() but it triggers the function during pageload which is not what I need.
aspx
<script type="text/javascript" src="external.js"></script>
输入按钮。
<input class="data" type="submit" name="cmdSubmit" value="Run Report" onclick="return FormValidate();"
onserverclick="RunReport" runat="server" id="Submit1" />
external.js
$(document).ready(function () {
function FormValidate() {
alert("Validated");
});
最佳答案
请删除 函数 FormValidate()
周围存在的 $(document).ready(function () {
使其成为全局函数。现在将其放入 ready
处理程序,您正在将其设置为本地作用域函数。因此您的 external.js 应该是:
function FormValidate() {
alert("Validated");
}
关于javascript - 当函数位于外部 javascript 文件中时 onclick 事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38736212/