Jquery,从函数访问变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4100524/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:39:49  来源:igfitidea点击:

Jquery , access variable from function

jqueryvariablesclosures

提问by mIRU

I have this code

我有这个代码

function getSelectData(id) {
    jQuery(id).change(function () {
        var value='';
        jQuery(id+" option:selected").each(function () {
            value =jQuery(this).val() ; 
        });
        console.log(value);
    });
    return value;
}

var d = getSelectData("#sort_post_date");
console.log(d);

How i can access variable "value" , i tried different methods , but nothing , where is console.log(value); , value exit , but outside nothing , Thank You for Helping !

我如何访问变量“值”,我尝试了不同的方法,但没有,console.log(value) 在哪里;,valueexit,但是out外什么都没有,谢谢帮忙!

回答by Hogan

You need ot move value outside of the function so it is bound to the closure. Like this:

您不需要将值移动到函数之外,以便它绑定到闭包。像这样:

function getSelectData(id) {
    var value='';

    // set value to be the current selected value
    value = jQuery(id+" option:selected").val();

    // change value whenever the box changes
    jQuery(id).change(function () {
        value = jQuery(id+" option:selected").val();
        console.log("I see a change! -> "+value);
    });

    console.log(value);
    return value;
}

var d = getSelectData("#sort_post_date");
console.log(d);

Here is the fiddle to show it works : http://jsfiddle.net/ZvuMh/

这是显示它有效的小提琴:http: //jsfiddle.net/ZvuMh/

回答by harpo

This is a classic closure question. Here are some of the most similar ones

这是一个经典的闭包问题。以下是一些最相似的

Event handlers inside a Javascript loop - need a closure?

Javascript 循环内的事件处理程序 - 需要关闭吗?

javascript timer or intervals created in loop using closure

使用闭包在循环中创建的 javascript 计时器或间隔

Javascript closure inside loops - simple practical example

循环内的 Javascript 闭包 - 简单的实际示例

Bottom line, the valuevariable is not local to the event handlers you're creating. So allof the handlers you create are going to have the value that valuehad when the outer function ended. In this case, it is still blank.

最重要的value是,该变量不是您正在创建的事件处理程序的本地变量。因此,您创建的所有处理程序都将具有value外部函数结束时的值。在这种情况下,它仍然是空白的。

回答by Hugo Migneron

You cannot access your value variable outside of the function it was declared in. This is called the variable scope. Read up on variable scope here

您不能在声明它的函数之外访问您的值变量。这称为变量范围。在这里阅读变量范围

If you want to be able to access a variable, you need to be in a scope that is at least as general as the one it was declared in.

如果您希望能够访问一个变量,您需要处于一个至少与声明它的范围一样通用的范围内。

So in this case :

所以在这种情况下:

function getSelectData(id){
    var value='';        
    jQuery(id).change(function () {
        jQuery(id+" option:selected").each(function () {
            value =jQuery(this).val() ; 
        });

        console.log(value);
    });
    return value;
}

var d=getSelectData("#sort_post_date");
console.log(d);

回答by jmans

Try definining the value variable in the global scope:

尝试在全局范围内定义 value 变量:

var value='';

function getSelectData(id) {
    jQuery(id).change(function () {
        value='';
        jQuery(id+" option:selected").each(function () {
            value =jQuery(this).val() ; 
        });
    console.log(value);
    });
    return value;
}

There's also no point in returning 'value' here, since it's being set in an event handler...there won't be anything in there (or, just an old value) at the point of return.

这里也没有返回“值”的意义,因为它是在事件处理程序中设置的......在返回点那里不会有任何东西(或者只是一个旧值)。