简体   繁体   中英

javascript variable concatenation in loop

How am I supposed to concatenate this?

here's my javascript code

var c0 = document.all.ntext.value; 
var c1 = document.all.stext.value;
var x;

for(x=0; x<2; x++)
{
    a.innerHTML = c //contatenation needed
}
var c0 = document.all.ntext.value; 
var c1 = document.all.stext.value;
var x;

for(x=0; x<2; x++)
{
    a.innerHTML = c0 + с1
}

Is that what you want?

It'll be much better if you'll go this way:

var c = [document.all.ntext.value, document.all.stext.value];
var x;

for(x=0; x<c.length; x++)
{
    a.innerHTML += c[0];
}
var needed = ['ntext', 'stext'];   
a.innerHTML = needed.map(function(key) {
  return document.all[key].value;
}).join('');

.map need a shim for old browsers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM