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 mentstore=new Array();
function creating(id,day)
{
if(mentstore[day,id] != null)
{
alert("It already exists: mentstore["+day+"]["+id+"]"+mentstore[day,id] );
var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"] ["+id+"]' value='"+mentstore[day,id]+"'/></div>
<div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
}
else
{
var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"] ["+id+"]' /></div>
<div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
$('#m').html(textinput);
}
function closement(id,day)
{
m.style.visibility='hidden';
var str='m['+day+']['+id+']';
var element = document.getElementById(str);
if(element.value !=null)
{
mentstore[day,id]=element.value;
alert('New values stored: mentstore['+day+']['+id+']'+mentstore[day,id]);
}
}
So in the above code if mentstore[0,0]='man' then mentstore[1,0] and [2,0] and [3,0] ....[7,0] are also filled with 'man'. Same thing is happening with mentstore[0,1] even mentstore[4,1] scenarios. Can any one please provide any tutorial or sample code where we can dynamically create javascript 2d arrays. Thanks in advance.
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 mentstore=new Array();
function creating(id,day)
{
if(mentstore[day,id] != null)
{
alert("It already exists: mentstore["+day+"]["+id+"]"+mentstore[day,id] );
var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"] ["+id+"]' value='"+mentstore[day,id]+"'/></div>
<div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
}
else
{
var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"] ["+id+"]' /></div>
<div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
$('#m').html(textinput);
}
function closement(id,day)
{
m.style.visibility='hidden';
var str='m['+day+']['+id+']';
var element = document.getElementById(str);
if(element.value !=null)
{
mentstore[day,id]=element.value;
alert('New values stored: mentstore['+day+']['+id+']'+mentstore[day,id]);
}
}
So in the above code if mentstore[0,0]='man' then mentstore[1,0] and [2,0] and [3,0] ....[7,0] are also filled with 'man'. Same thing is happening with mentstore[0,1] even mentstore[4,1] scenarios. Can any one please provide any tutorial or sample code where we can dynamically create javascript 2d arrays. Thanks in advance.
Share Improve this question edited Dec 28, 2009 at 15:34 Enjoy coding asked Dec 28, 2009 at 15:16 Enjoy codingEnjoy coding 4,36612 gold badges43 silver badges50 bronze badges 2-
[day,id]
is equivalent to just doing[id]
– Eli Grey Commented Dec 28, 2009 at 15:45 - Thank you all for helping. It is now working fine. I need to verify it more. Thanks again.. – Enjoy coding Commented Dec 28, 2009 at 15:58
3 Answers
Reset to default 6Replace mentstore[day,id]
with mentstore[day][id]
. That's the syntax for multi-dimensional arrays.
Use mentstore[0][0] instead of mentstore[0,0]. Also, use strict paraison whenever loose paraison is not needed:
var mentstore = [];
function creating(id,day)
{
if(mentstore[day] === undefined) mentstore[day] = [];
if(mentstore[day][id] !== undefined)
{
alert("It already exists: mentstore["+day+"]["+id+"]"+mentstore[day][id] );
var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"] ["+id+"]' value='"+mentstore[day][id]+"'/></div>
<div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
}
else
{
var textinput="<div id='closeit'>Comments:<input type='text' name='m["+day+"] ["+id+"]' /></div>
<div id='closing' onclick='closement("+id+","+day+")'>:)</div>";
$('#m').html(textinput);
}
function closement(id,day)
{
m.style.visibility='hidden';
var element = document.getElementById(str);
if(element.value !== '')
{
mentstore[day][id]=element.value;
alert('New values stored: mentstore['+day+']['+id+']'+mentstore[day][id]);
}
}
edit: in the original code, str is undefined and the execution fails. You can fix it in closement with:
var element = $('#closeit > input').eq(0);
JavaScript only supports arrays with a single index but the index can be anything, so if you really want two dimensional arrays, do this:
mentstore[day+','+id] = ...
i.e. use a string with to ponents as the key.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231700a4617705.html
评论列表(0条)