将跨度显示更改为只读输入

时间:2015-09-01 19:44:51

标签: javascript

我有一个计时器倒计时显示在<span>,我需要它输出到只读<input>,所以我可以用值来计算。有没有办法改变这个?

我试过

document.getElementById("timedown").value = display;

我尝试了各种其他东西,这些东西变得混乱而且无法正常工作。以下是功能和输入

function startTimer(duration, display) {
var start = Date.now(),
    diff,
    minutes,
    seconds;
function timer() {
    diff = duration - (((Date.now() - start) / 1000) | 0);

    minutes = (diff / 60) | 0;
    seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.textContent = minutes + ":" + seconds; 

    if (diff <= 0) {
        // faaopoopo se tasi sekone

        start = Date.now() + 1000;
    }
};
// amata vave
timer();
setInterval(timer, 1000);
}

window.onload = function () {  
var fiveMinutes = 60 * 10,
    display = document.querySelector('#time');

startTimer(fiveMinutes, display);
};
<div class="transtime">
  <form>
    <input type="text" id="timedown" value="0" readonly/>
  </form>
</div>

2 个答案:

答案 0 :(得分:1)

Here is a working codepen 将功能更改为:

    function timer() {
    diff = duration - (((Date.now() - start) / 1000) | 0);

    minutes = (diff / 60) | 0;
   seconds = (diff % 60) | 0;

    minutes = minutes < 10 ? "0" + minutes : minutes;
    seconds = seconds < 10 ? "0" + seconds : seconds;

    display.value= minutes + ":" + seconds; 

    if (diff <= 0) {
        // faaopoopo se tasi sekone

        start = Date.now() + 1000;
    }
};

更改值而不是文本值,它应该可以正常工作

答案 1 :(得分:1)

更改

display.textContent = minutes + ":" + seconds; 

document.getElementById("timedown").value = minutes + ":" + seconds; 

或更改

display = document.querySelector('#timedown');

display.value = minutes + ":" + seconds;