如何更新Javascript Global Array?

时间:2014-12-19 15:22:32

标签: javascript jquery arrays global-variables javascript-objects

我无法访问更新的全局数组(a1),它显示一个空数组。

<script type="text/javascript">

 var video_object = { title1:"", desc:"", link1:"",url1:""};
 var a1 = [];

 alert(a1.toSource()); // Inital array Value
$(function() {

    $("#upload1").click(function(){

        video_object.title1 = $("#title1").val();       
        video_object.desc = $("#desc1").val();                  
        video_object.link1 = $("#extra1").val();    
        video_object.url1  = $("#path1").val();     
        a1.push(video_object);

        alert(a1.toSource()); // Array Value updated initially (but gets null value afterwards)
    }); });


</script>

2 个答案:

答案 0 :(得分:0)

似乎函数Object.prototype.toSource()仅支持Firefox。

MDN

答案 1 :(得分:0)

我想你想这样做:

<script type="text/javascript">
  var a1 = [];

  alert(a1.toSource()); // Inital array Value
  $(function() {

    $("#upload1").click(function(){
      var video_object = { title1:"", desc:"", link1:"",url1:""};

      video_object.title1 = $("#title1").val();     
      video_object.desc = $("#desc1").val();                    
      video_object.link1 = $("#extra1").val();  
      video_object.url1  = $("#path1").val();       
      a1.push(video_object);

      alert(a1.toSource());
    });
  });
</script>