如何从顶部输入到底部输入获取值?
旧代码是
<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>
答案 0 :(得分: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 :(得分:1)
答案 2 :(得分:1)
我认为你只需要替换
lblValue.innerText
到
lblValue.value
答案 3 :(得分: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>