我在另一个函数里定义了一个全局变量si,我想在另一个函数里的URL里传递这个全局变量要怎么写,下面是我的错误写法var cart_url = 'flow.php?step=cart&co=si';

解决方案 »

  1.   

    var cart_url = 'flow.php?step=cart&co='+si;
      

  2.   

    不行啊,链过去的参数还是co=si
      

  3.   

    是的,我是在函数外面声明的,我另外定义了一个局部变量,返回的是null
      

  4.   

    var cart_url = 'flow.php?step=cart&co='+si;
    这个正解
      

  5.   

    那你就看下函数的执行顺序有问题没,应该是先执行给si赋值的函数
    然后再调用si
    给你个例子:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <body>
    <script>
    var si;
    function fun1(num){
    si=num;
    }
    function fun2(){
    c='flow.php?step=cart&co='+si;
    alert(c);
    }
    </script>
    <input type="button" value="测试" onclick="fun1(3);fun2();" />
    </body>
    </html>