函数没有在JavaScript中调用

时间:2012-01-30 08:22:32

标签: javascript

以下是我的javascript代码。问题是当我调用send()函数时它并没有真正响应因为x,y和z因为它们是局部变量。我尝试使用一些方法使它们全局化,比如创建一个对象,但都失败了。这里的任何人都知道这样做的正确方法吗?

<script type='text/javascript'>
  function store(x,y,z) {           
      var date = x+" "+y+" "+z;
      document.getElementById("date").value=date;
  }
  function send(){
      var events=document.getElementById("event").value;
      location.href="q.php?day=" + x + "&month=" + y + "&year=" + z;        
  }
</script>

3 个答案:

答案 0 :(得分:2)

尝试此操作,考虑到您在store

之前调用send函数
<script type='text/javascript'>
   var xx, yy, zz; //These are your global variables
   function store(x,y,z) {   
      xx = x;
      yy = y;
      zz = z;          
      var date = x + " " + y + " " + z;
      document.getElementById("date").value = date;
   }

   function send(){
      var events = document.getElementById("event").value;
      location.href="q.php?day=" + xx + "&month=" + yy + "&year=" + zz;      
   }
</script>

答案 1 :(得分:1)

您可以在store函数中创建URL,然后只需要一个全局变量:

<script type='text/javascript'>

  var url;

  function store(x, y, z) {           
    var date = x + " " + y + " " + z;
    document.getElementById("date").value = date;
    url = "q.php?day=" + x + "&month=" + y + "&year=" + z;
  }

  function send(){
    var events = document.getElementById("event").value;
    location.href = url;
  }

</script>

答案 2 :(得分:0)

更改函数发送和存储的名称

<script type='text/javascript'>
    var xGlobal,yGlobal,zGlobal;
    function mystore(x,y,z) {      
        xGlobal=x;
        yGlobal=y;
        zGlobal=z;       
        var date = x+" "+y+" "+z;

        document.getElementById("date").value=date;
    }

    function mysend(){
        var events=document.getElementById("event").value;
        location.href="q.php?day=" + xGlobal + "&month=" + yGlobal + "&year=" + zGlobal;      
    }
</script>

请参阅the example in fiddle