<input type="text" id="Amount" />
<lable id="copy_Amount" > </lable> $
“金额”输入中写的任何内容,都需要写在标签中
指出已接受答案中的一些明显缺陷。主要是
onkeyup
和innerHTML
的使用。
<input type="text" id="Amount" />
<!-- there is no lable tag. also label is not used as label so use span instead -->
<span id="copy_Amount"></span>
<script>
//can input text without keyup
document.getElementById("Amount").oninput = function(){
//no need for another lookup - use this
let stringValue = this.value;
//do not use innerHTML due to html injection
document.getElementById("copy_Amount").textContent = stringValue
}
</script>
使用
keyup
事件
正确的标签
label
使用
text()
设置标签文本
$('#Amount').keyup(function() {
$('#copy_Amount').text($('#Amount').val());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="Amount" />
<label id="copy_Amount" > </label> $
您可以使用 JQuery 来完成此操作。
$('#Amount').change(function() {
$('#copy_Amount').text($('#Amount').val());
});
import { useState } from "react";
function SoQuestion() {
const [state, setstate] = useState();
return (
<>
<input
type="text"
value={state}
placeholder="`enter code here`Type something..."
id="inp"
onInput={function(e){
setstate(e.target.value)
}}
/>
<label htmlFor="">{state}</label>
</>
);
}
export default SoQuestion;
你可以试试这个
<script>
document.getElementById("Amount").onkeyup = function () {
var stringValue = document.getElementById("Amount").value;
document.getElementById("copy_Amount").textContent = stringValue;
}
</script>