简体   繁体   English

“函数”在Javascript中没有定义错误:调用我的函数的正确方法是什么?

[英]“function” is not defined error in Javascript: what is the right way to call my function?

I understand a bit because of this post: JQuery, setTimeout not working why it's not working, but keeping everything this way, what is the right way to call _finalvalidate() inside my div? 我理解了一下因为这篇文章: JQuery,setTimeout不工作为什么它不起作用,但保持这一切,在我的div中调用_finalvalidate()的正确方法是什么?

 <script type="text/javascript">

$(document).ready(function(){

//On Submitting
function _finalvalidate(){
    alert('ppp');
    if(validateName() & validateEmail() & validatePhone()){
        alert('OK');
        return true
    }
    else{
        alert('false');
        return false;
    }
}
});

</script>


<div onclick="_finalvalidate();"> Take action </div>

The jQuery way of doing it would be something like: jQuery的做法是这样的:

<script type="text/javascript">

    $(document).ready(function(){

        //When clicking on the div, execute this 
        $("#validation").click(function() {
            alert('ppp');
            if(validateName() & validateEmail() & validatePhone()){
                alert('OK');
                return true
            }
            else{
                alert('false');
                return false;
            }
        });
    });

</script>
....
<div id="validate"> Take action </div>

If you really want to use the javascript function style, you gonna have to put the function outside the document.ready() function and then you'll be able to call it with the onclick attribute: 如果你真的想使用javascript函数样式,你必须将该函数放在document.ready()函数之外,然后你就可以使用onclick属性调用它:

<script type="text/javascript">

    function _finalvalidate(){
        alert('ppp');
        if(validateName() & validateEmail() & validatePhone()){
            alert('OK');
            return true;
        }
        else{
            alert('false');
            return false;
        }
    }   


</script>
....
<div onclick="_finalvalidate();"> Take action </div>

In this case you don't have anymore jQuery involved. 在这种情况下,您不再涉及jQuery。

You should bind your events with jQuery's events, if you are using jQuery. 如果您使用的是jQuery,则应该使用jQuery的事件绑定事件。 But as far as why it doesn't work, here is some info about JS scoping rules. 但至于为什么它不起作用,这里有一些关于JS范围规则的信息。


Any function that you declare to be used later on should NOT be in the jquery document ready callback. 您声明稍后要使用的任何函数都不应该在jquery文档就绪回调中。 You can't get it because it's hidden inside a function, which provides it's own private scope that you cannot get to from outside. 你无法得到它,因为它隐藏在一个函数中,它提供了你自己无法从外部获取的私有范围。

var f = function() {
  var inner = function() {};
};
inner(); // out of scope

You could export it to the global object, making it available everywhere. 您可以将其导出到全局对象,使其随处可用。

var f = function() {
  window.inner = function() {};
};
inner(); // works!

but the best way is to simply declare it in the global scope in the first place. 但最好的方法是首先在全球范围内宣布它。

var f = function() {}; // Now this function has no purpose anymore at all!
var inner = function() {};
inner(); // works

Your function doesn't exist in the scope of the window where you're trying to call it. 您的功能不存在于您尝试调用它的窗口范围内。 I would change your code to: 我会将您的代码更改为:

<div id="action">Take Action</div>

And then your JavaScript becomes: 然后你的JavaScript变成:

<script type="text/javascript">

$(document).ready(function(){

    //On Submitting
    $('#action').click(function() {
        alert('ppp');
        if(validateName() & validateEmail() & validatePhone()){
            alert('OK');
            return true
        } else {
            alert('false');
            return false;
        }
    });

</script>

If you want your method to be called from the div's onclick attribute, you'll need to make it available in the global scope. 如果您希望从div的onclick属性调用您的方法,则需要在全局范围内使其可用。 Below are some options. 以下是一些选项。 However, I should point out that Option 1 is generally frowned upon these days and Option 2 is just silly. 但是,我应该指出,选项1在这些日子里通常不赞成,而选项2只是愚蠢的。 The recommended approach for this kind of thing is to wire up your event handlers unobtrusively, as in Cory's answer. 对于这种事情,推荐的方法是不加掩饰地连接你的事件处理程序,就像在Cory的答案中那样。

Option 1 选项1

<script type="text/javascript">

function _finalvalidate(){
    alert('ppp');
    if(validateName() & validateEmail() & validatePhone()){
        alert('OK');
        return true
    }
    else{
        alert('false');
        return false;
    }
}

</script>


<div onclick="_finalvalidate();"> Take action </div>

Option 2 选项2

<script type="text/javascript">

var _finalvalidate;

$(document).ready(function() {
    _finalvalidate = function(){
        alert('ppp');
        if(validateName() & validateEmail() & validatePhone()){
            alert('OK');
            return true
        }
        else{
            alert('false');
            return false;
        }
    };
});

</script>


<div onclick="_finalvalidate();"> Take action </div>

To keep it in jQuery, I'd give your div an id tag and call it using a jQuery onclick event. 为了保持它在jQuery中,我给你的div一个id标签并使用jQuery onclick事件调用它。 Also are you sure you want to be doing bitwise & operations in your if, or logical ands && ? 你确定要在if或者逻辑和&&进行按位&操作吗?

<script type="text/javascript">

$(document).ready(function() {
    $('#callme').click(function() {
        alert('ppp');
        if(validateName() && validateEmail() && validatePhone()){
            alert('OK');
            return true;
        } else {
            alert('false');
            return false;
        }
    }
});

</script>

<div id="callme"></div>

The issue with your code is that your function is not defined in the global context, but rather it's defined inside the document.ready callback function. 你的代码的问题是你的函数没有在全局上下文中定义,而是在document.ready回调函数中定义。 So, when the browser gets the onclick and does an eval of your string, it doesn't find that function name in the global context. 因此,当浏览器获取onclick并对字符串进行eval时,它在全局上下文中找不到该函数名称。 You can fix it a number of different ways: 您可以通过多种不同方式修复它:

1) Change your function to be defined in the global scope like this: 1)将您的函数更改为在全局范围内定义,如下所示:

//On Submitting
window._finalvalidate = function(){

2) Move your _finalvalidate() function outside of the document.ready() so it's in the global scope. 2)将您的_finalvalidate()函数以外document.ready()所以它在全球范围内。 There is no reason for it to be in document.ready() because it is not executing right away. 它没有理由在document.ready()因为它没有立即执行。

3) Change to the "jQuery" way of specifying event handlers rather than putting onclick=xxx in your HTML as Cory has illustrated in his answer. 3)更改为指定事件处理程序的“jQuery”方式,而不是像Cory在他的答案中说明的那样在你的HTML中放入onclick = xxx。

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

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