显示所有值的Javascript数组

时间:2012-11-28 09:41:18

标签: javascript arrays

Hello People这里是我的代码,

function send()
{
  var param_count=document.getElementsByName('eqt_param[]');

  for (var i=0; i<param_count.length; i++)
  {
    var test=param_count[i].value;  
    var param_value='Eqt_Param'+i+'='+test;

    alert(param_value);
  }
}

如果我发出警报,我会"Eqt_Param0=4.00"然后"Eqt_Param1=3.00",但我想立即提醒,输出应该像"Eqt_Param0=4.00,Eqt_Param1=3.00 "之后警告这样我还要删除','介于两者之间如何解决这个问题?

4 个答案:

答案 0 :(得分:3)

你的意思是:

function send()
{
  var param_count=document.getElementsByName('eqt_param[]');
  var values = [];
  for (var i=0; i<param_count.length; i++)
  {
    values.push('Eqt_Param'+i+'='+param_count[i].value)
  }
  alert(values.join(', '));
}

答案 1 :(得分:0)

function send()
{
  var tempArray=[]; 
  var param_count=document.getElementsByName('eqt_param[]');

  for (var i=0; i<param_count.length; i++)
  {
     var test=param_count[i].value;  
     var param_value +='Eqt_Param'+i+'='+test;
     tempArray.push(param_value)

  }

    alert(tempArray.join(','));  //to join with ','
    var joinedstr=tempArray.join(',');
    var finalArray= joinedstr.split(',');  //to split with ','
}

答案 2 :(得分:0)

Array有一些有用的功能可以让您的生活更轻松,forEachjoin

function send()
{
  var toPrint = []
  document.getElementsByName('eqt_param[]').forEach( function(el, idx) {
    toPrint.append('Eqt_Param'+idx+'='+x.value);
  }
  alert(toPrint.join(', '));
}

答案 3 :(得分:0)

您不需要数组:

alert((function(array, string, _i, _len) {
    for(; _i < _len; _i++)
        string += 'Eqt_Param' + _i + ' = '+ array[_i].value + ", ";
    return string.substr(0, string.length -2);
    } (document.getElementsByName('eqt_param[]'), "", 0, document.getElementsByName('eqt_param[]').length)));