简体   繁体   中英

Javascript array displaying all values

Hello People here is my code,

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);
  }
}

if i alert i get "Eqt_Param0=4.00" then "Eqt_Param1=3.00" but i want to alert at once output should be something like "Eqt_Param0=4.00,Eqt_Param1=3.00 " after alerting this way i also want to remove the ',' in between how to fix this?

Do you mean this:

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(', '));
}
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 ','
}

Array has some useful functions to make your life easier, forEach and join .

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

You do not need an array for that:

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)));

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