javascript - 替换html5中文本框的输出文本

标签 javascript jquery css html

如何替换 html5 中的输出文本。

例如,用户输入这个文本:我的名字是托尼。

我想把输出文本:“Toni”替换成Ani,比如输出,变成:“我叫Ani”。

这是我的代码:

<title>textBoxes.html</title>
 <script type = "text/javascript">
  // from textBoxes.html
  function repleace(){
  var txtName = document.getElementById("txtName");
  var txtOutput = document.getElementById("txtOutput");
  var name = txtName.value;
  if(name == "toni"){
  name = "ani";
  }
  txtOutput.value = name;
  }
 </script>
 <link rel = "stylesheet"
   type = "text/css"
   href = "textBoxes.css" />
 </head>
 <body>
 <h1>Text Box Input and Output</h1>
 <form action = "">
  <fieldset>
  <label>Type your name: </label>
  <input type = "text"
    id = "txtName" />
  <input type = "button"
    value = "GO"
    onclick = "repleace()"/>
  <input type = "text"
    id = "txtOutput" />
  </fieldset>
 </form>
 </body>
</html>

这段代码只是在我输入文本时替换文本:Toni,但是当我输入:我的名字是 Toni 时。无法替换文本,Toni。

最佳答案

使用替换功能。

name.replace("toni", "Ani");

关于javascript - 替换html5中文本框的输出文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35896936/

相关文章:

javascript - Google map API - 单击折线时禁用双击缩放

javascript - 如何递归调用promise函数

jquery - 如何使用 jQuery 在多个 ID 上设置属性?

android - 如何启用放大元标记?

Jquery 'gallery' 交互导致图像链接损坏

javascript - 当 Lotus Notes 中的字段发生更改时,如何刷新表单?

javascript - 是否可以仅使用客户端创建txt文件? ...但是到特定文件夹

jquery - Jqgrid表格编辑出错

jquery - Firefox 中的切换功能出现问题(两个正在切换的 div 内有 wufoo 脚本)

css - 将带有 svg 和文本的 div 居中