简体   繁体   English

根据输入的值在文本字段中自动添加第一个字符

[英]Auto add first Char in Textfield based on Entered Value

I am attempting to add a textfield called Initials and based on what user enters in say fName textfield onkeyup auto complete initials textfield using first character only.我正在尝试添加一个名为 Initials 的文本字段,并根据用户输入的内容说 fName 文本字段 onkeyup 仅使用第一个字符自动完成缩写文本字段。

Here's my pathetic attempt, need help这是我可怜的尝试,需要帮助

<!DOCTYPE html>
<html>
<body>

<p>&nbsp;</p>
<p>Enter your name: 
  <input name="fName" type="text" id="fname" onkeyup="showFirstChar()">
</p>
<p>Initials: 

  <input name="Initials" type="text" id="Initials">    
  <script>
    function showFirstChar() {
        var sWord = document.getElementById('fName').innerHTML;
        document.getElementById('Initials').innerHTML = sWord.charAt(0);
    }
  </script>
  
</p>
</body>
</html>

try this尝试这个

var sWord = document.getElementById('fName').value;
document.getElementById('Initials').value= sWord.charAt(0);

Input has .value property instead of .textContent输入具有.value属性而不是.textContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM