如何将变量从一个脚本传递到另一个脚本 [英] how do I pass a variable from one script to another

查看:181
本文介绍了如何将变量从一个脚本传递到另一个脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个我无法正常工作的脚本。
它的目的是检查复选框是否被选中,然后调用第二个响应的
例程,这取决于我最初调用脚本时所显示的selectedindex(sindex)的内容

Here is a script I can't get to work correctly. It's purpose is to check whether a check box is checked and then to call a second routine that responds, dependent on what the selectedindex (sindex) was shown to be when I originally called the script

<script type="text/javascript">
function checkB(ctrl,sindex) {  //get the state of the check box 
var sindex = {
    0: 0,
    1: 1,
    2: 2,
    3: 3
 };


if (ctrl.checked == true) { 
return function( which ) {
replaceContentmainobjectOn(sindex [which]);
} else {    
if (ctrl.checked == false) { 
replaceContentmainobjectOff();
}   
} 
}
</script>

这是第二个叫做的脚本

var replaceContentmainobjectOn =(function() {
var info = {
    0: 2,
    1: 1,
    2: 2,
    3: 3
 };

    return function( which ) {
    document.getElementById('ecwid-productoption-8840317-mainobject').selectedIndex = ( info[ which ] ) ;
};

}())

这就是我所说的第一个例行程序

This is what I'm calling the first routine with

onclick="checkB(this,sindex);


推荐答案

两个人< script> 块共享相同的执行范围,全局范围。您在一个< script> 中的全局范围内创建的所有变量都可以在另一个中访问。

Two individual <script> blocks share the same execution scope, the global scope. All variables you create in the global scope inside one <script> are accessible in the other.

<script>
    var a = 5;
</script>

<script>
    alert( a );
</script>

同样适用于函数。

<script>
    var b = function( c ){ return c; }
</script>

<script>
    alert( b(12) );
</script>

你可以排除,你的问题似乎存在于第一个脚本中,这不是语法上的有效。

That you can rule-out, your problem seems to lay in the first script, which is not syntactically valid.

这篇关于如何将变量从一个脚本传递到另一个脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆