我正在写一个小练习,我将事情从一个“选择”移动到另一个。效果很好!我还没有多选,但我稍后会这样做。更重要的是,我想我会添加计数器,以便可以添加与“选项”相关的值。该功能似乎不起作用。当我将事物从一列移到另一列时,用于求和值的标签将填充文本 0[object Object]。我确信这是因为一个非常明显的错误,但我缺乏用任何其他术语表达问题的词汇。请教我?:-)
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<style type="text/css">
#selectSource {
width: 320px;
}
#selectTarget {
width: 320px;
}
#wgtsum {
width: 22px;
}
#cubesum {
width: 22px;
}
</style>
<title>Loader</title>
<script>
var data;
var sumCube = 0;
var sumWgt = 0;
window.onload = function () {
//Add way to load JSON data from a text file or something
var load = document.getElementById('selectSource')
data = [
{ num: "1", cube: 6, wgt: 2, title: "num: 1 cube: 6 wgt: 2" },
{ num: "2", cube: 4, wgt: 4, title: "num: 2 cube: 4 wgt: 4" },
{ num: "3", cube: 2, wgt: 6, title: "num: 3 cube: 2 wgt: 6" }
];
for (i = 0; i < data.length; i++) {
load.options.add(new Option(data[i].title, data[i].num));
}
}
function sum() {
var sumTarget = document.getElementById('selectTarget');
for (i = 0; i < data.length; i++) {
for (o = 0; o < sumTarget.length; o++) {
if (sumTarget[o].value = data[i].value) {
sumCube = sumCube + data[i].cube;
sumWgt = sumWgt + data[i].wgt;
}
}
}
document.getElementById("cubesum").setAttribute("value", sumCube);
document.getElementById("wgtsum").setAttribute("value", sumWgt);
}
function move(from, to) {
selected = from.options.selectedIndex;
to.options.add(new Option(from.options[selected].text, from.options[selected].value));
from.options.remove(selected);
sum();
}
</script>
</head>
<body>
<form>
<select id="selectTarget" size="4">
</select>
<button type="button" onclick="move(this.form.selectSource, this.form.selectTarget)" style="width: 58px">To Target</button>
<button type="button" onclick="move(this.form.selectTarget, this.form.selectSource)" style="width: 58px">To Source</button>
<select id="selectSource" size="4">
</select>
<input value="0" type="text" id="cubesum" size="5"/>
<input value="0" type="text" id="wgtsum" size="5"/>
</form>
</body>
</html>