简体   繁体   English

如何将变量传递给另一个要调用的函数 - Javascript

[英]How to pass a variable into another function to be called - Javascript

I am trying to get this to work: 我想让这个工作:

<!DOCTYPE html>
<html>
<body>

<form><input type='button' id='confirm' value="Confirm"/></form>
<p id="getvariable"></p>

<script> 
function a(){var t= 5;}
function b(){return t;}

document.getElementById('confirm').onclick = function(){
document.getElementById("getvariable").innerHTML = b();}

</script>
</body>

but I can't pass a variable to the other function. 但我无法将变量传递给另一个函数。 Any help would be great! 任何帮助都会很棒!

The problem is that the variable t is in the scope of the function a when you set it. 问题是变量t在设置时属于函数a的范围。 It needs to be global. 它需要是全球性的。 Like this: 像这样:

var t = 0;
function a(){ t = 5;}
function b(){return t;}

document.getElementById('confirm').onclick = function(){
    document.getElementById("getvariable").innerHTML = b();
}

Demo here 在这里演示

You can't use a value in another function that has been defined locally in a particular function. 您不能在已在特定函数中本地定义的另一个函数中使用值。

So this is wrong: 所以这是错的:

function a(){var t= 5;}
function b(){return t;}

What I would do is : 我会做的是:

var t=0;
function a(){t= 5;}
function b(){return t;}

document.getElementById('confirm').onclick = function(){
document.getElementById("getvariable").innerHTML = b();}

Here's a link that can help you : http://www.w3schools.com/js/js_scope.asp 这是一个可以帮助您的链接: http//www.w3schools.com/js/js_scope.asp

t valriable should be decalred outside the function scope t valriable应该在功能范围之外进行decalred

    var t = 0;
    function a(){
      t = 5;
    }
    function b(){
      return t;
    }

    document.getElementById('confirm').onclick = function(){
      document.getElementById("getvariable").innerHTML = b();
   }

暂无
暂无

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

相关问题 如何将$(this)变量传递给另一个函数javascript - how to pass $(this) variable into another function javascript 在字符串中(在 JavaScript 中)调用函数时,如何将变量传递给函数? - How to pass a variable to a function when the function is being called in a string (in JavaScript)? 如何将变量传递给另一个在 Promise 链中调用的文件中的异步函数 - How to pass variable to an async function in another file called in Promise chain 如何将 JavaScript 变量值传递给另一个 JavaScript 函数? - how to pass JavaScript variable value to another JavaScript function? 如何将javascript动态变量传递给另一个javascript中的函数? - How to pass javascript dynamic variable to a function in another javascript? 如何在javascript中将变量从一个函数传递给另一个函数 - how to pass variable from one function to another function in javascript 如何在JavaScript中将局部变量从一个函数传递给另一个函数 - how to pass local variable from one function to another function in javascript 如何将变量从一个 function 传递到另一个 Javascript? - How to pass variable from one function to another Javascript? 如何在从另一个JavaScript函数调用的JavaScript函数中传递多个参数? - How to pass multiple parameter in javascript function which is called from another javascript function? 调用javascript函数时,将javascript变量值传递给php文件 - Pass javascript variable value to a php file when javascript function is called
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM