function $(v){return document.getElementById(v);}
function check(){
}
function StringBuffer(){
this._strings_ = new Array();
}
StringBuffer.prototype.append = function(str){
this._strings_.push(str);
};
StringBuffer.prototype.toString = function(txt){
return this._strings_.join(txt);
}
function getval(tar){var buffer = new StringBuffer();
id1=tar+"1";
id2=tar+"2";
id3=tar+"3";
a=$(id1).value; b=$(id2).value; c=$(id3).value;
if(b||c){
if(b&&c){ buffer.append(a);
buffer.append(b);
buffer.append(c);
}
else if(b&&!c){buffer.append(a);
buffer.append(b);
}
else{buffer.append(a);
buffer.append(c);}$(tar).value = buffer.toString(",");
}
else{$(tar).value=a;}
}<input id="position1" type="text" onblur="getval('position')" />
<input id="position2" type="text" onblur="getval('position')" />
<input id="position3" type="text" onblur="getval('position')" />
<input name=position id="position" type="text" />
其实就是将三个输入框的值用逗号分开,然后赋给position, 看别人用数组来连接字符串提高效率,我也学着用,但是貌似这样很繁琐,能不能简化一下?
尤其是for循环时循环次数较多的情况 效率是明显的 毕竟+=操作js需要做6步事情 但像上边的例子 没有必要 直接+=
function getval(tar)
{
var buffer = "";
id1 = tar + "1";
id2 = tar + "2";
id3 = tar + "3";
a = $(id1).value;
b = $(id2).value;
c = $(id3).value;
if (b || c)
{
if (b && c)
{
buffer += (a+','+b+','+c);
}
else if (b && !c)
{
buffer += (a+','+b);
}
else
{
buffer += (a+','+c);
} $(tar).value = buffer;
}
else
{
$(tar).value = a;
}
}
return $(this).attr("text");
}).get().join(",");返回某组条件标签的值用,分割