2012-06-20 23 views
1

我正在定义一个javascript数组,它将持有一个被选中的复选框的行号和列号,如果复选框被选中,我会将行号和列号的值分配给1 ,否则我会把0JavaScript数组中存储的值

我需要做一些基于检查和取消选中复选框的操作并将1,0存储在数据库中。

这是我想要做的。

var sampleArray=new Array(); 

function setAll(column, value) { 

     var i, n = column.length; 
     for (i = 0; i <= n; ++i) { 
      column[i] = value; 
     } 
     } 

if (//some condition){ 
    if(cell is checked) 
     setAll(sampleArray,1); // here i am calling a function and setting the value in index to 1; 
    } else if (cell is unchecked) 
    { 
     setAll(sampleArray,0); 
    } 

上面代码的问题是它没有存储所有操作的值(选中和取消选中)。

有人能帮我解决吗?是否有可能在JavaScript内创建一个hashmap类型的函数?

H<key, value> 

,其中关键是山坳没有和行不行,和值是0或1

+1

Java与此有何关系? –

+0

您可能想要返回修改后的数组,或者只是不发送数组,因为它看起来像一个全局变量。 – GreenGiant

+0

@DavidB接受我的道歉 – Namita

回答

0
function setAll(column, value) { 

    var i, n = column.length; 
    for (i = 0; i <= n; ++i) { 
     column[i] = value; 
    } 
return (column) 
    } 

if (//some condition){ 
if(cell is checked) 
    sampleArray= setAll(sampleArray,1); // here i am calling a function and setting the value in index to 1; 
} else if (cell is unchecked) 
{ 
    sampleArray= setAll(sampleArray,0); 
} 

,或者如果你想使用全局变量

function setAll(value) { 

    var i, n = sampleArray.length; 
    for (i = 0; i <= n; ++i) { 
     sampleArray[i] = value; 
    } 
    } 

if (//some condition){ 
if(cell is checked) 
setAll(1); // here i am calling a function and setting the value in index to 1; 
} else if (cell is unchecked) 
{ 
    setAll(0); 
} 

以检查一个复选框是否被选中查看Javascript to check whether a checkbox is being checked or unchecked