jQuery jquery函数的返回值

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

return value from jquery function

jquery

提问by kvu787

Possible Duplicate:
jQuery ajax return value

可能的重复:
jQuery ajax 返回值

I have this jQuery script:

我有这个 jQuery 脚本:

$('#headerSubmit').click(function() {
    var source = $('#header').attr('value');
});

that gets the valueattribute of the element and stores it in the sourcevariable.

获取value元素的属性并将其存储在source变量中。

How do I return the sourcevariable to use in the rest of the script?

如何返回source要在脚本其余部分中使用的变量?

Thanks.

谢谢。

回答by JCOC611

As you would be setting the sourcevariable after a click, any code that uses that variable should be executed only after such event. Therefore, you should paste your code right after it:

正如您将source在单击后设置变量一样,任何使用该变量的代码都应仅在此类事件之后执行。因此,您应该在其后立即粘贴您的代码:

$('#headerSubmit').click(function() {
    var source = $('#header').attr('value');
    // Rest of the code here
});

Otherwise, the variable wouldn't be initialized before a click.

否则,该变量不会在单击之前初始化。

Edit:Note all other answers incite you to declare the variable on global scope. This is useless unless your code executes after a click since the variable would be null.

编辑:请注意所有其他答案都会促使您在全局范围内声明变量。这是无用的,除非您的代码在单击后执行,因为变量将为空。

回答by Santhiya

Declare 'source' as global.

将“源”声明为全局。

That is..

那是..

var source;
//your piece of jquery code
$('#headerSubmit').click(function() {
   source = $('#header').attr('value');
});

回答by darthwade

var source;
$('#headerSubmit').click(function() {
    source = $('#header').attr('value');
});

回答by Nishu Tayal

Define var source outside of this click script. and use like this :

在此单击脚本之外定义 var 源。并像这样使用:

var source;
$('#headerSubmit').click(function() {
    source = $('#header').attr('value');
});

In this way 'source' will have global scope, and can be used anywhere with the assigned value later on.

通过这种方式,'source' 将具有全局作用域,以后可以在任何具有指定值的地方使用。

回答by David Stetler

You can return a variable with

您可以返回一个变量

 return source;

Sounds like you probably are just trying to call that variable later in the script. This is a problem with scope. If you take out the var and just declare

听起来您可能只是想稍后在脚本中调用该变量。这是范围的问题。如果你取出 var 并声明

source = $('#header').attr('value');

You will be able to call that variable in other parts of your script.

您将能够在脚本的其他部分调用该变量。

回答by Makita

If you want source to be used in the entire script, declare it outside the function:

如果要在整个脚本中使用 source,请在函数外声明:

var source = null;
$('#headerSubmit').click(function() {
    source = $('#header').attr('value');
});

回答by Hkachhia

var source;
$('#headerSubmit').click(function() {
    source = $('#header').attr('value');
   return source;
});

回答by RobB

To declare a variable in the global scope, you must initialize it before you try setting it in a function, otherwise the declaration is only applicable to the local scope. If you get your scopes wrong then your references will be invalid and you can expose yourself to bigger issues.

要在全局作用域中声明变量,您必须在尝试在函数中设置它之前对其进行初始化,否则该声明仅适用于局部作用域。如果您的范围错误,那么您的引用将无效,您可能会面临更大的问题。

In this case, you will be unable to reference the variables value elsewhere in your script as you are intending to do since the variable only exists within your function currently.

在这种情况下,您将无法按照您的意图在脚本中的其他地方引用变量值,因为该变量当前仅存在于您的函数中。

Per MDN:

每个MDN

When you declare a variable outside of any function, it is called a global variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called a local variable, because it is available only within that function.

当您在任何函数之外声明一个变量时,它被称为全局变量,因为它可用于当前文档中的任何其他代码。当您在函数内声明一个变量时,它被称为局部变量,因为它仅在该函数内可用。

var source;
$('#headerSubmit').click(function() {
    source = $('#header').attr('value');
});

回答by UltraInstinct

The way you are using it is asynchronous. You are setting a callback which is triggered on the "click" event. To have it available to the rest of the script, you have two options.

您使用它的方式是异步的。您正在设置一个在“点击”事件上触发的回调。要使其可用于脚本的其余部分,您有两个选择。

  1. use a global variable. (using this is bad sometimes though)

    var myGlobalVariable;
    $('#headerSubmit').click(function() {
        var myGlobalVariable = $('#header').attr('value');
    });
    
  2. Call the function that needs this value inside the callback.

    function myAnotherFunction(val){
         alert("I received" + val + ". I will update my set of"
              +" variables without putting it as global variable");
    }
    
    $('#headerSubmit').click(function() {
        myAnotherFunction($('#header').attr('value'));
    });
    
  1. 使用全局变量。(虽然有时使用它是不好的)

    var myGlobalVariable;
    $('#headerSubmit').click(function() {
        var myGlobalVariable = $('#header').attr('value');
    });
    
  2. 在回调中调用需要此值的函数。

    function myAnotherFunction(val){
         alert("I received" + val + ". I will update my set of"
              +" variables without putting it as global variable");
    }
    
    $('#headerSubmit').click(function() {
        myAnotherFunction($('#header').attr('value'));
    });