由于我有几个条目需要除以我提供的数字(num1 变量),所以我发现完成任务很困难。
我希望循环将我除以 34,并将名为 listX 的每个变量的值存储起来。有 9 个列表(每个列表都有不同的数字),我希望每个列表都用我提供的数字除以,但要先除以2,然后是 3,然后是 4、5……直到我提供的数字。
我发现最好用对象来完成,但我找不到解决方案,因为我对这些东西没有那么丰富的经验。理想的情况是获得如下对象:
列表1:第1师:xxx
列表1:第二分区:xxx
列表1:第34师:xxx
...
名单8:第22师:xxx
...
列表9:第33分区:xxx 名单9:第34师:xxx
“xth division”应与我在 num1 变量中提供的数字链接
我想要得到的是这样的:
它询问我希望我的值被除多少次,我输入 5。然后它询问我 9 个输入的值是多少。
我声明第一个输入100。我想要的结果应该是:
100/1 = 1
100/2 = 50
100/3 = 33,33
100/4 = 25
100/5 = 20
然后我声明第二个输入数字200。结果应该是:
200/1 = 200
200/2 = 100
200/3 = 66,66
200/4 = 50
200/5 = 40
它一直向上直到我最后输入......
我正在尝试开发的是用于议会席位分配的 D'Hondt 方法,某些国家正在使用该方法。
这是我到目前为止所得到的,但是ofc,它不起作用。
var array1 = [];
var list = "list";
function someFunction() {
var num1 = 34;
for (var i = 1; i <= num1; ++i) {
for (var j = 1; j <= 9; j++) {
array1[j] += document.getElementById(list + j).value / i;
array1[j] += "<br/>";
}
}
document.getElementById("demo1").innerHTML = list + j;
}
<tr>
<td><input type="text" id="list1" style="width:50px" onkeyup="someFunction()"></input></td>
<td><input type="text" id="list2" style="width:50px" onkeyup="someFunction()"></input></td>
<td><input type="text" id="list3" style="width:50px" onkeyup="someFunction()"></input></td>
<td><input type="text" id="list4" style="width:50px" onkeyup="someFunction()"></input></td>
<td><input type="text" id="list5" style="width:50px" onkeyup="someFunction()"></input></td>
<td><input type="text" id="list6" style="width:50px" onkeyup="someFunction()"></input></td>
<td><input type="text" id="list7" style="width:50px" onkeyup="someFunction()"></input></td>
<td><input type="text" id="list8" style="width:50px" onkeyup="someFunction()"></input></td>
<td><input type="text" id="list9" style="width:50px" onkeyup="someFunction()"></input></td>
</tr>
最佳答案
我认为这就是您正在寻找的内容,但有一些注释。
一些注意事项:
input
元素没有结束标记。type="text
是input
元素的默认值,因此您不需要 来写它。- 不要重复自己(干)。由于所有
input
元素都需要 相同的样式,只需设置一个 CSS 规则即可对所有样式进行样式设置 同样的方式。 - 不要使用内联 HTML 事件处理程序。将 JavaScript 与 您的 HTML 并遵循现代标准。
有关详细信息,请参阅下面的评论。
// Get the number to divide by (You can get this anyway you want. A prompt is show here.):
var num = +prompt("How many calculations per input would you like?");
// Get all the table inputs into an array
var inputs = Array.prototype.slice.call(document.querySelectorAll("td > input[id^='input']"));
// Loop over the input array...
inputs.forEach(function(input){
// Set up an event handler for the input
input.addEventListener("input", someFunction);
});
function someFunction(){
var resultString = "";
for(var i = 0; i < num; i++){
// Do the math and build up the string
resultString += (this.value / (i + 1)) + "<br>";
}
// Update the output cell that corresponds to the input element with the results
document.querySelector("#" + this.id.replace("input", "output")).innerHTML = resultString;
}
td > input { width:50%; }
<table id="inout">
<tr>
<td><input id="input1"></td>
<td><input id="input2"></td>
<td><input id="input3"></td>
<td><input id="input4"></td>
<td><input id="input5"></td>
<td><input id="input6"><td>
<td><input id="input7"></td>
<td><input id="input8"></td>
<td><input id="input9"></td>
</tr>
<tr>
<td id="output1"></td>
<td id="output2"></td>
<td id="output3"></td>
<td id="output4"></td>
<td id="output5"></td>
<td id="output6"><td>
<td id="output7"></td>
<td id="output8"></td>
<td id="output9"></td>
</tr>
</table>
<div id="output"></div>
关于javascript - 动态改变变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50220173/