简体   繁体   English

如何将变量从一个脚本传递到另一个脚本

[英]how do I pass a variable from one script to another

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 它的目的是检查是否选中了复选框,然后调用响应的第二个例程,这取决于我最初调用脚本时所显示的selectedindex(sindex)的内容

<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>

here is the second script that is called 这是第二个被调用的脚本

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);

Two individual <script> blocks share the same execution scope, the global scope. 两个单独的<script>块共享相同的执行范围,即全局范围。 All variables you create in the global scope inside one <script> are accessible in the other. 您在一个<script>中的全局范围内创建的所有变量都可以在另一个中访问。

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

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

Same applies to functions. 同样适用于功能。

<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. 你可以排除,你的问题似乎在第一个脚本,这在语法上是无效的。

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

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