如何将文本从一个文本框复制到标签

问题描述 投票:0回答:5
<input type="text" id="Amount" />
<lable id="copy_Amount" > </lable> $

“金额”输入中写的任何内容,都需要写在标签中

javascript jquery
5个回答
3
投票

指出已接受答案中的一些明显缺陷。主要是

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>


0
投票
  1. 使用

    keyup
    事件

  2. 正确的标签

    label 

  3. 使用

    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> $


0
投票

您可以使用 JQuery 来完成此操作。

$('#Amount').change(function() {
  $('#copy_Amount').text($('#Amount').val());
});

0
投票
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;

-2
投票

你可以试试这个

 <script>
       document.getElementById("Amount").onkeyup = function () {
        var stringValue = document.getElementById("Amount").value;
        document.getElementById("copy_Amount").textContent = stringValue;
     }
  </script>
© www.soinside.com 2019 - 2024. All rights reserved.