javascript 未定义未捕获的 ReferenceError 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17445771/
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 function is not defined
提问by Chankey Pathak
I have a variable named as myVar
. Its value changes when you click on the checkbox. When the checkbox is clicked you'll get an alert box showing its value as 1. When you deselect it, it will again show a alert box with value 0. This is correct.
我有一个名为myVar
. 单击复选框时,它的值会发生变化。单击复选框时,您将看到一个警报框,其值显示为 1。当您取消选择它时,它将再次显示一个值为 0 的警报框。这是正确的。
Now I have 2 questions.
现在我有2个问题。
When I try to submit the document by clicking on
Submit
then I get an error asUncaught ReferenceError: confirm_submit is not defined
. Why?When I put the
confirm_submit
function out of theready event
I don't get the error but then in that case the second alert box which is inside confirm_submit function showsundefined
formyVar
. Why? Is themyVar
not accessible withinconfirm_submit
function?
当我尝试通过单击提交文档时,
Submit
我收到一个错误为Uncaught ReferenceError: confirm_submit is not defined
. 为什么?当我将
confirm_submit
函数从 中取出时,ready event
我没有收到错误,但在这种情况下,confirm_submit 函数内的第二个警报框显示undefined
为myVar
. 为什么?是myVar
不是在confirm_submit
函数内不可访问?
Code:
代码:
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.treeTable.js"></script>
<script type="text/javascript">
var myVar;
jQuery(document).ready(function() {
$("#myTable2").treeTable({
expandable: true,
clickableNodeNames: true,
initialState: "expanded",
});
document.getElementById('myVar').addEventListener('click', myVar, false);
function myVar() {
if (document.getElementById('myVar').checked === true) {
myVar = 1;
} else {
myVar = 0;
}
alert(myVar);
}.................
some functions....................
function confirm_submit() {
alert(myVar);
if (confirm('Press OK to confirm')) {
document.myform.myVar.value = myVar;
document.myform.submit();
reload_parent_and_close_window();
}
}
and some more functions.....................
});
</script>
</head>
<body>
<form name="myform" id="myform" action="$action" method="post" onsubmit="return false;">
<input type="checkbox" name="myVar" id="myVar" value="myVar">Text here
</form>
<a href="javascript:confirm_submit()">Submit</a>
</body>
</html>
回答by Craig
You seem to be failing to grasp a few fundamental concepts here, some JavaScript some programming basics.
您似乎没有掌握一些基本概念,一些 JavaScript 一些编程基础知识。
function myVar() {
...
alert(myVar);
}
What did you expect to happen here? myVar the function and myVar the variable in this scope are the same thing. If you declare a variable and then declare a function with the same name, the function will replace the variable in the stack. Also, there is no block scope in JavaScript. Everything declared in a function is declared first by the compiler, regardless of block. So...
你期望在这里发生什么?myVar 函数和 myVar 这个作用域中的变量是一回事。如果你声明了一个变量,然后又声明了一个同名的函数,该函数将替换堆栈中的变量。此外,JavaScript 中没有块作用域。函数中声明的所有内容首先由编译器声明,而不管块如何。所以...
function a() {
var a = 1;
if (1) {
var b = 4;
alert(b);
}
alert(b);
}
Don't assume scoping is the same as Java or C++.
不要假设范围与 Java 或 C++ 相同。
Also, if you want to make something explicitly global, then make it explicit. Try renaming the myVar function to something sensible like "onClick_myVar". Then immediately before where the function is declared, put the function inside a closure and declare your state variable:
另外,如果你想让某些东西显式地成为全局的,那就让它显式。尝试将 myVar 函数重命名为诸如“onClick_myVar”之类的合理名称。然后紧接在声明函数的位置之前,将函数放入闭包中并声明您的状态变量:
(function() { // Closure
var myVar;
function onClick_myVar() {
if ($(this).getattr("checked")) {
myVar = 1;
} else {
myVar = 0;
}
alert(myVar);
}
$('#myVar').click(onClick_myVar);
})();
回答by Craig
function myVar() {
if (document.getElementById('myVar').checked === true) {
myVar = 1;
} else {
myVar = 0;
}
alert(myVar);
}
That will only work once. As soon as you enter the function myVar
you will replace it's value (function is an object in JS) with either 1 or 0, and it will no longer be a function object, but a number.
那只会工作一次。一旦你输入函数,myVar
你就会用 1 或 0 替换它的值(函数是 JS 中的一个对象),它不再是一个函数对象,而是一个数字。
回答by bipen
the problem is... you have same name for your var and the input element..
问题是......你的 var 和 input 元素的名称相同..
document.myform.myVar.value = myVar;
so either change your input element's id to someother variable or change all your myVar name
因此,要么将输入元素的 id 更改为其他变量,要么更改所有 myVar 名称
<input type="checkbox" name="myVar" id="inputId" value="myVar">Text here
document.myform.inputId.value = myVar;