请帮助修复它。问题出在哪里。我试图将#minimum的值增加11倍,并按所示顺序写入其属性。
<input id="minimum" type="number" value="">
<button onclick="Calculate()">Calculate</button>
<script>
var minimum = document.getElementById("minimum").value;
var one = min;
var two = min++;
var three = min++;
var four = min++;
var five = min++;
var six = min++;
var seven = min++;
var eight = min++;
var nine = min++;
var ten = min++;
var eleven = min++;
var twelve = min++;
function Calculate(){
document.write(one+','+three+','+five+','+seven+','+nine+','+eleven+','+six+','+four+','+two+','+twelve+','+ten+','+eight);
}
</script>
答案 0 :(得分:1)
变量min
在任何地方都没有定义。您可以在minimum
当前所在的任何地方使用变量min
,或将minimum
的初始声明更改为min
。
<input id="minimum" type="number" value="">
<button onclick="Calculate()">Calculate</button>
<script>
var min = document.getElementById("minimum").value;
var one = min;
var two = min++;
var three = min++;
var four = min++;
var five = min++;
var six = min++;
var seven = min++;
var eight = min++;
var nine = min++;
var ten = min++;
var eleven = min++;
var twelve = min++;
function Calculate(){
document.write(one+','+three+','+five+','+seven+','+nine+','+eleven+','+six+','+four+','+two+','+twelve+','+ten+','+eight);
}
</script>```
答案 1 :(得分:1)
像这样更改您的Calculate()
函数;
<script>
function Calculate() {
var minimum = document.getElementById("minimum").value;
let min = parseInt(minimum);
var one = min;
var two = min++;
var three = min++;
var four = min++;
var five = min++;
var six = min++;
var seven = min++;
var eight = min++;
var nine = min++;
var ten = min++;
var eleven = min++;
var twelve = min++;
document.write(one + ',' + three + ',' + five + ',' + seven + ',' + nine + ',' + eleven + ',' + six + ',' + four + ',' + two + ',' + twelve + ',' + ten + ',' + eight);
}
您所做的是在执行开始时获取输入值。但是在该输入中输入数字值之后,这些值将不会更新,因此您需要将该代码放入Calculate()函数中
答案 2 :(得分:1)
您过于复杂了。
将所需的订单存储到数组中。无论您在字段中输入什么数字,.join(', ')
ed 字符串将始终是该数字的序列 和顺序(由于数组基于0,因此减去1
)
const el_min = document.getElementById("minimum"), // Cache your DOM Elements
el_btn = document.getElementById("calculate"),
el_res = document.getElementById("result"),
order = [1, 3, 5, 7, 9, 11, 6, 4, 2, 12, 10, 8]; // Store your expected order
function calculate() {
const minVal = parseInt(el_min.value, 10); // Use parseInt with radix!
el_res.textContent = order.map(ord => minVal + ord - 1).join(', ');
}
el_btn.addEventListener('click', calculate);
<input id="minimum" type="number" value="1">
<button id="calculate">Calculate</button>
<div id="result"></div>
关于您的主要问题,min
不是不是 minimum
,因此是一个错字,您应该在单击时进行数学运算,而不是准备文档。
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt
https://developer.mozilla.org/en/docs/Web/API/Node/textContent
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/join
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener