2016-09-19 33 views
0

我試圖在另一個禁用的輸入框中實時顯示輸入的結果,並沒有太大的運氣。代碼如下:在輸入框中顯示實時結果

var inputBox = document.getElementById('input'); 

inputBox.onkeyup = function(){ 
    document.getElementById('output').innerHTML = inputBox.value*2; 
} 

<input type='text' id='input'> 

<input type='text' id='output' disabled> 

任何人都可以幫忙嗎?

感謝,

約翰

回答

1

嘗試添加監聽器和使用價值,而不是innerHTML的的:

document.getElementById('input').addEventListener("keyup", myFunction); 

var inputBox = document.getElementById('input'); 

function myFunction(){ 
    document.getElementById('output').value = inputBox.value*2; 
} 

的jsfiddle:https://jsfiddle.net/22t37834/

+0

當我運行這段代碼時,出現以下錯誤:null不是一個對象(evaluate.getElementByid('input')。addEventListener') –

+0

好吧,我現在正確地命令了代碼並正在工作。謝謝 –

0

,因爲它是進入焦炭之後觸發使用 'KEYUP'。

$("#input").keyup (function (e) { 
    $('#output').val($(this).val() * 2); 
}); 
0

嘗試這一個 -

var inputBox = document.getElementById('input'); 
 

 
inputBox.onkeyup = function(){ 
 
    document.getElementById('output').innerHTML = inputBox.value*2; 
 
    var val = document.getElementById('input').value 
 
    document.getElementById('output').value = val; 
 
}
<input type='text' id='input'> 
 

 
<input type='text' id='output' disabled>