输入字段关联数组

时间:2011-05-04 13:08:15

标签: javascript

让我们说,我有两个输入字段,它们的值:val1和val2。 (他们是字符串)

如何将它们转换为关联数组:{val1:va​​l2}第一个值为键,第二个值为值?

提前致谢!

2 个答案:

答案 0 :(得分:2)

var foo = {};
foo[document.getElementById('an_input').value] = document.getElementById('another_input').value

答案 1 :(得分:2)

// get the key and the value
var key = document.getElementById('input1').value;
var val = document.getElementById('input2').value;

// and store 'em
var obj = {};     // need an empty object first, as you
obj[key] = val;   // can't store a property on a null value