2014-10-03 59 views
1

如何從頂部輸入值到底部輸入值?如何從頂部輸入值到底部輸入值?

舊代碼

<span id="lblValue"></span> 

,但我嘗試添加值輸入文本類型它無法工作

<input type="text" id="lblValue" value=""> 

我該怎麼辦?

http://jsfiddle.net/VDd6C/744/

<script> 
    function edValueKeyUp() 
    { 
     var edValue = document.getElementById("edValue"); 
     var s = edValue.value; 

     var lblValue = document.getElementById("lblValue"); 
     lblValue.innerText = "The text box contains: "+s; 

     //var s = $("#edValue").val(); 
     //$("#lblValue").text(s);  
    } 
</script> 
+1

這是不清楚你真正想要的東西。 – Daan 2014-10-03 14:16:05

+0

''實時值''?你是什​​麼意思? – hindmost 2014-10-03 14:16:31

+0

我想他想輸入並立即更新標籤。 – DontVoteMeDown 2014-10-03 14:17:14

回答

3

input沒有一個innerText屬性 - 它有一個.value屬性。

lblValue.value= "The text box contains: "+s; 

演示:http://jsfiddle.net/VDd6C/747/

總是有你的控制檯打開,你會看到錯誤:Uncaught NoModificationAllowedError: Failed to set the 'innerText' property on 'HTMLElement': The 'input' element does not support text insertion.

1

我想你只需要更換

lblValue.innerText

lblValue.value 
1

由於lblValue也是輸入文本,使用.value進行設置:

<input id="edValue" type="text" onKeyUp="edValueKeyUp()"><br> 
<span id="label"></span><br/> 
<input type="text" id="lblValue" value=""> 
<script> 
function edValueKeyUp() { 
    var edValue = document.getElementById("edValue"); 
    var s = edValue.value; 

    var lblValue = document.getElementById("lblValue"); 
    document.getElementById("label").innerText = "The text box contains: "; 
    lblValue.value = s; 
      //^.value not .innerText 
    lblValue.readOnly = true; // optional 

} 
</script>