【问题标题】:Javascript change text of all inputsJavascript更改所有输入的文本
【发布时间】:2013-04-03 15:05:44
【问题描述】:

我真的是 Web 开发的新手,我正在尝试使用 Javascript 更改一些输入的文本。这是我的代码必须执行的示例

<!DOCTYPE html>
<html>
<body>

<p>Click the button to replace "R$" with "" in the field below:</p>

<input id="demo" value="R$ 1223,43"></input>
<input id="demo1" value="R$ 134523,67"></input>
<input id="demo2" value="R$ 12453,41"></input>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var x=document.getElementByTagName("input")

for(var i = 0; i < x.length; i++) {

  var str=x[i].innerHTML; 

  var n=str.replace(",",".");
  var n1 = n.replace("R$ ","");

  document.getElementById("demo").innerHTML=n1;
 }


}
</script>

</body>
</html>

所以,我想撤回“R$”并将“,”替换为“。”对于一些数学运算。我必须对代码中的所有输入执行此操作。

【问题讨论】:

  • .value,而不是 .innerHTML - 我也强烈推荐 MDN 而不是 w3fools.com

标签: javascript html input replace getelementsbytagname


【解决方案1】:

这些是必需的步骤 - 至少是第 1 步到第 3 步

  1. 将脚本移到它所属的头部
  2. 将 getElementByTagName 更改为 getElementsByTagName,复数
  3. 获取和更改 x[i].value
  4. 链接替换

DEMO

<!DOCTYPE html>
<html>
<head>
<title>Replace example</title>
<script>
function myFunction() {
  var x=document.getElementsByTagName("input"); // plural

  for(var i = 0; i < x.length; i++) {
    var str=x[i].value; 
    x[i].value=str.replace(",",".").replace("R$ ","");
  }
 }
</script>
</head>

<body>

<p>Click the button to replace "R$" with "" in the field below:</p>

<input id="demo" value="R$ 1223,43"></input>
<input id="demo1" value="R$ 134523,67"></input>
<input id="demo2" value="R$ 12453,41"></input>

<button onclick="myFunction()">Try it</button>


</body>
</html>

【讨论】:

    【解决方案2】:

    你就快到了,替换了一些东西让它看起来像这样:

    function myFunction() {
        var x = document.getElementsByTagName("input"); // ; was missing and you used getElementByTagName instead of getElementsByTagName
    
        for (var i = 0; i < x.length; i++) {
    
            var str = x[i].value; // use .value
    
            var n = str.replace(",", ".");
            var n1 = n.replace("R$ ", "");
    
            //document.getElementById("demo").innerHTML=n1; // use x[i] again instead
            x[i].value = n1; // and again use .value
        }
    }
    

    DEMO - 运行更新的代码


    【讨论】:

    • 工作就像一个魅力。非常感谢
    【解决方案3】:

    首先,使用 .value 而不是 .innerHTML。 .innerHTML 指的是标签打开和关闭内的文本。

    其次,更正 var x=document.getElementByTagName("input") 处的拼写 它应该是 getElementsByTagName

    【讨论】:

      【解决方案4】:

      这个函数应该做你想做的:

      function myFunction()
      {
          var eles=document.getElementsByTagName("input");
      
          for(var i = 0; i < eles.length; i++) 
          {
              if(eles[i].type != 'text') continue; // inputs that aren't of type text dont make sense here
              var str = eles[i].value; 
              str=str.replace(",",".");
              str=str.replace("R$ ","");
              eles[i].value=str;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 2011-06-26
        • 2012-05-13
        • 1970-01-01
        相关资源
        最近更新 更多