【发布时间】:2016-02-29 17:33:46
【问题描述】:
我在下面制作了 html 来显示基于可变因素的时间。 如果没有任何选择,结果应显示为 1 小时 30 分钟。 有了选择,时间会更短。
问题:我无法从函数中获取结果时间以显示在输入类型框中。
对于新手的错误,我很抱歉,因为这是我的第一个脚本
function Clear() {
document.calculation.reset();
document.calculation.buff[0].focus();
}
function Calculate() {
// First check if only 2 crafting products are selected
if (document.calculation.buff[3].checked &&
document.calculation.buff[4].checked) {
alert('does not work');
document.calculation.time0.value = '';
document.calculation.time1.value = '';
document.calculation.time2.value = '';
document.calculation.time3.value = '';
document.calculation.time4.value = '';
}
else {
factor = 60000;
if (document.calculation.buff[0].checked) {
factor = factor * 0.8;
}
if (document.calculation.buff[1].checked) {
factor = factor * 0.5;
}
if (document.calculation.buff[2].checked) {
factor = factor * 0.9;
}
if (document.calculation.buff[3].checked) {
factor = factor * 0.95;
}
if (document.calculation.buff[4].checked) {
factor = factor * 0.95;
}
if (document.calculation.stable[0].checked) {
time0 = msTime(90);
document.calculation.time0.value = time0;
document.calculation.time0.focus();
}
else {
document.calculation.time0.value = '';
}
}}
// function milliseconds to time
function msTime(s) {
var ms = (s * factor) % 1000;
s = (s - ms) / 1000;
var secs = s % 60;
s = (s - secs) / 60;
var mins = s % 60;
var hrs = (s - mins) / 60;
return hrs + 'h ' + mins + 'm ' + secs + 's' + ms;
}
<form name="calculation" id="calculation" action="#">
<input type="checkbox" name="buff" value="0"> 0
<input type="checkbox" name="buff" value="1"> 1
<input type="checkbox" name="buff" value="2"> 2
<input type="checkbox" name="buff" value="3"> 3
<input type="checkbox" name="buff" value="4"> 4
<input type="button" value="Reset" id="pop" onclick="Clear()"></span><input type="button" value="Calculate" id="top" onclick="Calculate()">
<input type="checkbox" name="stable" value="0" checked> Result</td>
<input type="text" size="20" name="time0">
</form>
【问题讨论】:
-
抱歉忘记删除一个/td
-
您可能希望在每个
document.calculation之后添加.elements。例如。document.calculation.elements.stable.checked -
您在 javascript 控制台中使用非常具体且易于理解的错误编写错误代码。你解决了吗?
-
.elements 不起作用。
-
我在 Google Chrome 控制台中只看到一个错误。我知道我在某处犯了一些基本错误:(抱歉,这是我的第一个脚本。实际上这是我的第二个脚本,但基于第一个脚本。第一个确实有效,但不够准确。不知道如何添加该部分,它不适合此文本字段
标签: javascript function input time types