本文介绍了Javascript 2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在javascript中使用二维数组来存储字符串。但是我无法正确获取值。以下是我的代码。

I am trying to use 2 dimensional array in javascript for storing strings. But I am not able to get the values correctly. Below is my code.

      var commentstore=new Array();
        function creating(id,day)
        {
            if(commentstore[day,id] != null)
         {
             alert("It already exists: commentstore["+day+"]["+id+"]"+commentstore[day,id] );
             var textinput="<div id='closeit'>Comments:<input type='text' name='comm["+day+"]  ["+id+"]' value='"+commentstore[day,id]+"'/></div>
                               <div id='closing' onclick='closecomment("+id+","+day+")'>:)</div>";
         }
            else
            {
                var textinput="<div id='closeit'>Comments:<input type='text' name='comm["+day+"]  ["+id+"]' /></div>
                               <div id='closing' onclick='closecomment("+id+","+day+")'>:)</div>";
                $('#comm').html(textinput);
            }

    function closecomment(id,day)
    {
        comm.style.visibility='hidden';
        var str='comm['+day+']['+id+']';
        var element = document.getElementById(str);
     if(element.value !=null)
     {
      commentstore[day,id]=element.value;
      alert('New values stored: commentstore['+day+']['+id+']'+commentstore[day,id]);
     }
    }

所以在上面的代码中如果commentstore [0,0 ] ='man',然后commentstore [1,0]和[2,0]和[3,0] .... [7,0]也填满man。 commentstore [0,1]甚至是commentstore [4,1]的场景也是一样。任何人都可以提供任何教程或示例代码,我们可以动态创建javascript 2d数组。感谢提前。

So in the above code if commentstore[0,0]='man' then commentstore[1,0] and [2,0] and [3,0] ....[7,0] are also filled with 'man'. Same thing is happening with commentstore[0,1] even commentstore[4,1] scenarios. Can any one please provide any tutorial or sample code where we can dynamically create javascript 2d arrays. Thanks in advance.

推荐答案

使用commentstore [0] [0]代替commentstore [0,0]。另外,在不需要松动compareaison的情况下使用严格的比较:

Use commentstore[0][0] instead of commentstore[0,0]. Also, use strict comparaison whenever loose comparaison is not needed:

var commentstore = [];

function creating(id,day)
{
    if(commentstore[day] === undefined) commentstore[day] = [];
    if(commentstore[day][id] !== undefined)
    {
        alert("It already exists: commentstore["+day+"]["+id+"]"+commentstore[day][id] );
        var textinput="<div id='closeit'>Comments:<input type='text' name='comm["+day+"]  ["+id+"]' value='"+commentstore[day][id]+"'/></div>
                       <div id='closing' onclick='closecomment("+id+","+day+")'>:)</div>";
    }
    else
    {
        var textinput="<div id='closeit'>Comments:<input type='text' name='comm["+day+"]  ["+id+"]' /></div>
                       <div id='closing' onclick='closecomment("+id+","+day+")'>:)</div>";
        $('#comm').html(textinput);
    }

function closecomment(id,day)
{
    comm.style.visibility='hidden';
    var element = document.getElementById(str);
    if(element.value !== '')
    {
        commentstore[day][id]=element.value;
        alert('New values stored: commentstore['+day+']['+id+']'+commentstore[day][id]);
    }
}

编辑:在原始代码中, str 未定义,执行失败。你可以通过以下方法来解决它:在

edit: in the original code, str is undefined and the execution fails. You can fix it in closecomment with:

var element = $('#closeit > input').eq(0);

这篇关于Javascript 2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:40