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, 看别人用数组来连接字符串提高效率,我也学着用,但是貌似这样很繁琐,能不能简化一下?

解决方案 »

  1.   

    给他们相同的名字,然后获取的时候用document.getElementsByName就可以了
      

  2.   

    其实LZ的这段代码是替代字符串+= (var a = "abc"; a+="def" )操作的另一种实现方式从某种意义上说 这种方式可以提高效率 因为操作数组和操作字符串还是有区别的 
    尤其是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;
    }
    }
      

  3.   

     var text = $("#" + obj.RegionCheckBoxListPanel + " :checkbox[checked]").map(function () {
                return $(this).attr("text");
            }).get().join(",");返回某组条件标签的值用,分割