日期:2014-05-18  浏览次数:20624 次

请教下面两个javascript函数的作用,最好能够多做注释。谢谢
function   RefreshData()
{
InitRight(document.all.txtRight.value,   document.all.sltRight,   " ");
InitRight(document.all.txtAllRight.value,   document.all.sltAllRight,   document.all.ddlRightType.value);
}

function   InitRight(str,   obj,   type)
{
                  var   aValue   =   new   Array();
var   i,   j,   k,   m,   flag;

aValue   =   str.split( "$^ ");

j   =   aValue.length;
k   =   0;
obj.options.length   =   (j   -   (j   %   3))   /   3;

for(i=0;i <j;i+=3)
{
if(j   <   3)
break;

if(type   !=   " ")  
{
flag   =   true;
if(aValue[i]   !=   type)
{
if   (obj.options.length   >   0)
obj.options.length   =   obj.options.length   -   1;
continue;
}

for(m=0;m <document.all.sltRight.options.length;m++)
{
if(document.all.sltRight.options[m].value   ==   aValue[i+1])
{
if   (obj.options.length   >   0)
obj.options.length   =   obj.options.length   -   1;
flag   =   false;
      continue;
}
}

if(flag   ==   false)
{
continue;
}
}

obj.options[k].value   =   aValue[i+1];
obj.options[k].text   =   aValue[i+2];
k   +=   1;
}
}

------解决方案--------------------
function InitRight(str, obj, type)
{
var aValue = new Array();
var i, j, k, m, flag;
//字符串拆分,赋予数据aValue
aValue = str.split( "$^ ");
//数组长度
j = aValue.length;
k = 0;
//j除以3,取最小正整数
obj.options.length = (j - (j % 3)) / 3;
//for循环实现在 <select> 下拉项增加或删除数组aValue各项的值
for(i=0;i <j;i+=3)
{

if(j < 3)
break;

if(type != " ")
{
flag = true;
if(aValue[i] != type)
{
if (obj.options.length > 0)
obj.options.length = obj.options.length - 1;
continue;
}

for(m=0;m <document.all.sltRight.options.length;m++)
{
if(document.all.sltRight.options[m].value == aValue[i+1])
{
if (obj.options.length > 0)
obj.options.length = obj.options.length - 1;
flag = false;
continue;
}
}

if(flag == false)
{
continue;
}
}

obj.options[k].value = aValue[i+1];
obj.options[k].text = aValue[i+2];
k += 1;
}
}