问题描述
有人能给我一个带有多维输入数组的JavaScript示例/示例吗?希望你能提供帮助,因为我还是JavaScript的新手。
Can anyone give me a sample/example of JavaScript with a multidimensional array of inputs? Hope you could help because I'm still new to the JavaScript.
就像你输入2行2列时一样,它的输出将是2行输入和2行输入列。
Like when you input 2 rows and 2 columns the output of it will be 2 rows of input and 2 columns of input.
像这样:
[input][input]
[input][input]
推荐答案
var numeric = [
['input1','input2'],
['input3','input4']
];
numeric[0][0] == 'input1';
numeric[0][1] == 'input2';
numeric[1][0] == 'input3';
numeric[1][1] == 'input4';
var obj = {
'row1' : {
'key1' : 'input1',
'key2' : 'input2'
},
'row2' : {
'key3' : 'input3',
'key4' : 'input4'
}
};
obj.row1.key1 == 'input1';
obj.row1.key2 == 'input2';
obj.row2.key1 == 'input3';
obj.row2.key2 == 'input4';
var mixed = {
'row1' : ['input1', 'inpu2'],
'row2' : ['input3', 'input4']
};
mixed.row1[0] == 'input1';
mixed.row1[1] == 'input2';
mixed.row2[0] == 'input3';
mixed.row2[1] == 'input4';
如果你想存储DOM元素:
And if you're wanting to store DOM elements:
var inputs = [
[
document.createElement('input'),
document.createElement('input')
],
[
document.createElement('input'),
document.createElement('input')
]
];
inputs[0][0].id = 'input1';
inputs[0][1].id = 'input2';
inputs[1][0].id = 'input3';
inputs[1][1].id = 'input4';
在附加元素之前,不确定上述情况有多大。以下内容可能更符合您的要求:
Not real sure how useful the above is until you attach the elements. The below may be more what you're looking for:
<input text="text" id="input5"/>
<input text="text" id="input6"/>
<input text="text" id="input7"/>
<input text="text" id="input8"/>
var els = [
[
document.getElementById('input5'),
document.getElementById('input6')
],
[
document.getElementById('input7'),
document.getElementById('input8')
]
];
els[0][0].id = 'input5';
els[0][1].id = 'input6';
els[1][0].id = 'input7';
els[1][1].id = 'input8';
或者,这可能是:
<input text="text" value="4" id="input5"/>
<input text="text" value="4" id="input6"/>
<br/>
<input text="text" value="2" id="input7"/>
<input text="text" value="4" id="input8"/>
var els = [
[
document.getElementById('input5'),
document.getElementById('input6')
],
[
document.getElementById('input7'),
document.getElementById('input8')
]
];
var result = [];
for (var i = 0; i < els.length; i++) {
result[result.length] = els[0][i].value - els[1][i].value;
}
给出:
[2, 0]
在控制台中。如果你想将它输出到文本,你可以 result.join('');
,它会给你 2 0
。
In the console. If you want to output that to text, you can result.join(' ');
, which would give you 2 0
.
编辑
以及工作演示:
<input text="text" value="4" id="input5"/>
<input text="text" value="4" id="input6"/>
<br/>
<input text="text" value="2" id="input7"/>
<input text="text" value="4" id="input8"/>
<br/>
<input type="button" value="Add" onclick="add()"/>
// This would just go in a script block in the head
function add() {
var els = [
[
document.getElementById('input5'),
document.getElementById('input6')
],
[
document.getElementById('input7'),
document.getElementById('input8')
]
];
var result = [];
for (var i = 0; i < els.length; i++) {
result[result.length] = parseInt(els[0][i].value) - parseInt(els[1][i].value);
}
alert(result.join(' '));
}
这篇关于JavaScript多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!