使用表单元素展示关联数组
//获取表单元素的名称和值
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>