让我们说,我有两个输入字段,它们的值:val1和val2。 (他们是字符串)
如何将它们转换为关联数组:{val1:val2}第一个值为键,第二个值为值?
提前致谢!
答案 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