【问题标题】:Transfer input from prompt() into a text field将输入从 prompt() 传输到文本字段
【发布时间】:2019-01-29 15:43:12
【问题描述】:

我有一个 HTML 页面,当按下开始按钮时,会出现一个提示框,提示用户输入他/她的姓名。当用户输入他/她的名字并确认输入时,它会出现在黑色的盒子上(如代码所示),文本颜色为白色。我不确定如何将值从 prompt() “转移”到文本字段。我知道输入字段的值默认为空。

function myFunction() {
  var person = prompt("Please enter your name");
  if (person != null) {
       person = document.getElementById(input).value;
  }
}
#input{
  width: 15%;
  padding: 50px 50px;
  margin: 8px 0;
  background-color: #191818;
  color: white;

}

#input1 {
  width: 15%;
  padding: 50px 50px;
  margin: 8px 0;
  background-color: #7C7878;
  color: white;

}

.btnEdit{
    width: 30px;
    margin: 5px;
    display: block;

    font-weight:bold;  
    text-align:center;
    font-size:10px;   
    line-height:15px;
}
<h2>Part 3</h2>
<button onclick="return myFunction()" id="strt" value="Start">Start</button>
<button onclick="return reset()" id="clr" value="Clear">Clear</button> <br />
<br>
<input type="text" id="input" style="text-align:center;" name="input"/>
<button id="swap" value="Swap"  class="btnEdit">--></button>
<button id="swap1" value="Swap1"  class="btnEdit"><--</button>
<input type="text" id="input1" value="" style="text-align:center;" name="input1"/>

预期的输出将是用户在提示中输入他/她的姓名,并在确认后,他/她的姓名将以黑色和白色文本颜色出现在文本字段中。

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    你只需要交换它。

    person = document.getElementById(input).value;
    

    以上是错误的。应该是:

    document.getElementById("input").value = person;
    

    您还需要更新"input"。这不是一个常数。

    function myFunction() {
      var person = prompt("Please enter your name");
      if (person != null) {
        document.getElementById("input").value = person;
      }
    }
    #input {
      width: 15%;
      padding: 50px 50px;
      margin: 8px 0;
      background-color: #191818;
      color: white;
    }
    
    #input1 {
      width: 15%;
      padding: 50px 50px;
      margin: 8px 0;
      background-color: #7C7878;
      color: white;
    }
    
    .btnEdit {
      width: 30px;
      margin: 5px;
      display: block;
      font-weight: bold;
      text-align: center;
      font-size: 10px;
      line-height: 15px;
    }
    <h2>Part 3</h2>
    <button onclick="return myFunction()" id="strt" value="Start">Start</button>
    <button onclick="return reset()" id="clr" value="Clear">Clear</button> <br />
    <br>
    <input type="text" id="input" style="text-align:center;" name="input" />
    <button id="swap" value="Swap" class="btnEdit">--></button>
    <button id="swap1" value="Swap1" class="btnEdit"><--</button>
    <input type="text" id="input1" value="" style="text-align:center;" name="input1" />

    【讨论】:

    • @gaetanoM 感谢编辑伙伴。我也添加了 sn-p。
    • 天哪!谢谢!那里的菜鸟错误。
    • @alonelycoder 发生。永远记住那个会得到价值的人在左边。常量或表达式在右边。从技术上讲,简而言之,数据从右到左传播。
    【解决方案2】:

    function myFunction() {
      var person = prompt("Please enter your name");
      if (person != null) {
            document.getElementById('input').value = person;
      }
    }
    #input{
      width: 15%;
      padding: 50px 50px;
      margin: 8px 0;
      background-color: #191818;
      color: white;
    
    }
    
    #input1 {
      width: 15%;
      padding: 50px 50px;
      margin: 8px 0;
      background-color: #7C7878;
      color: white;
    
    }
    
    .btnEdit{
        width: 30px;
        margin: 5px;
        display: block;
    
        font-weight:bold;  
        text-align:center;
        font-size:10px;   
        line-height:15px;
    }
    <h2>Part 3</h2>
    <button onclick="return myFunction()" id="strt" value="Start">Start</button>
    <button onclick="return reset()" id="clr" value="Clear">Clear</button> <br />
    <br>
    <input type="text" id="input" style="text-align:center;" name="input"/>
    <button id="swap" value="Swap"  class="btnEdit">--></button>
    <button id="swap1" value="Swap1"  class="btnEdit"><--</button>
    <input type="text" id="input1" value="" style="text-align:center;" name="input1"/>

    【讨论】:

    • 那么这个答案比 20 分钟前发布的另一个答案更好吗?它看起来就像我的副本,没有解释,也没有。这是低质量和剽窃答案的一个例子。只是说说而已。
    猜你喜欢
    • 1970-01-01
    • 2011-04-11
    • 2020-03-14
    • 2021-11-22
    • 2011-09-22
    • 2010-10-24
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多