最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how do I pass a variable from one script to another - Stack Overflow

programmeradmin1浏览0评论

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>

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

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>

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);
Share Improve this question asked Mar 21, 2012 at 18:39 Larry PoulinLarry Poulin 291 gold badge1 silver badge6 bronze badges 3
  • Can you provide a plete working demonstration of your problem with a tool like jsfiddle? Also why are you returning function objects? – mellamokb Commented Mar 21, 2012 at 18:41
  • 1 The first script that you post doesn't look valid. – CAbbott Commented Mar 21, 2012 at 18:44
  • very wet behind the ears here.. I need to be able to supply the var info to the second routine when I call it from the first. How do you do that? Global Variables....??? It's very confusing being a noobie – Larry Poulin Commented Mar 21, 2012 at 19:05
Add a ment  | 

1 Answer 1

Reset to default 5

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>

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.

发布评论

评论列表(0)

  1. 暂无评论