使用表单元素展示关联数组

//获取表单元素的名称和值

function getValue(){

  var elems = document.getElementById('picker').elements;

  var elemArray = new Object();

  for(var i = 0; i < elems.length; i++){

    if(elems[i].type == 'text'){

      elemArray[elems[i].id] = elems[i].value;

    }

  }

  checkVals(elemArray);

  return false;

}

//检查值

function checkVals(elemArray){

  var str = '';

  for(var key in elemArray){

    str += key + ',' + elemArray[key] + ' ';

  }

  document.getElementById('result').innerHTML = str;

}

 

//HTML代码

<form id='picker' onsubmit='return getValue()'>

<input type='text' id='first' />

<input type='text' id='second' />

<input type='text' id='third' />

<input type='text' id='four' />

<input type='submit' id='Validate' />

</form>

<div id='result'></div>

posted @ 2013-05-12 23:15  Laborc_L  阅读(192)  评论(0编辑  收藏  举报