简体   繁体   English

未定义的ReferenceError函数未定义

[英]Uncaught ReferenceError function is not defined

I have a variable named as myVar . 我有一个名为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. 单击复选框后,您将获得一个警报框,其值为1。取消选择该框时,它将再次显示一个值为0的警报框。这是正确的。

Now I have 2 questions. 现在我有两个问题。

  1. When I try to submit the document by clicking on Submit then I get an error as Uncaught ReferenceError: confirm_submit is not defined . 当我尝试通过单击Submit文档时,由于Uncaught ReferenceError: confirm_submit is not defined我收到一个错误Uncaught ReferenceError: confirm_submit is not defined Why? 为什么?

  2. When I put the confirm_submit function out of the ready event I don't get the error but then in that case the second alert box which is inside confirm_submit function shows undefined for myVar . 当我将confirm_submit函数置于ready event我没有收到错误,但是在那种情况下,位于confirm_submit函数内部的第二个警报框显示myVar undefined Why? 为什么? Is the myVar not accessible within confirm_submit function? confirm_submit函数中无法访问myVar吗?

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>

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. 此范围内的myVar函数和myVar变量是同一件事。 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. 另外,JavaScript中没有块作用域。 Everything declared in a function is declared first by the compiler, regardless of block. 在函数中声明的所有内容均由编译器首先声明,而不管块是什么。 So... 所以...

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". 尝试将myVar函数重命名为“ onClick_myVar”等明智的名称。 Then immediately before where the function is declared, put the function inside a closure and declare your state variable: 然后在声明该函数之前,立即将该函数放入一个闭包中,并声明您的状态变量:

(function() { // Closure
    var myVar;

    function onClick_myVar() {
        if ($(this).getattr("checked")) {
            myVar = 1;
        } else {
            myVar = 0;
        }
        alert(myVar);
    }

    $('#myVar').click(onClick_myVar);
})(); 
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您将其值(函数是JS中的对象)替换为1或0,它将不再是函数对象,而是一个数字。

the problem is... you have same name for your var and the input element.. 问题是...您的var和输入元素名称相同。

 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM