javascript - 动态改变变量

标签 javascript arrays for-loop

由于我有几个条目需要除以我提供的数字(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="textinput 元素的默认值,因此您不需要 来写它。
  • 不要重复自己(干)。由于所有 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/

相关文章:

javascript - 如何使 js 警报仅在 php echo 消息上弹出,而不在其他所有内容上弹出?

javascript - 在Javascript中更新JSON数据的引用问题

python - 如何根据模糊条件从Numpy数组中选择值?

c++ - 如何从 const float* 中获取 C++ vector

php - 转置二维数组,用逗号连接第二层,用管道连接第一层

javascript - 如何只提交从下拉列表中选择的值?

java - 在 Java 中通过循环返回 double 值

java - 循环字符串和使用 str.replace 的意外输出

php - 如何检索数组中与查询关联的其他值?

javascript - 尽管有 $event.stopPropagation,但在 <a> 标记内单击时会发生重定向