简体   繁体   English

javascript这段代码出了什么问题

[英]javascript Whats wrong with this code

my textarea and all its atributes is correct, but my javascript not right cannot set value of oTextbox3. 我的textarea及其所有属性都是正确的,但我的javascript不正确无法设置oTextbox3的值。

<html>
  <head>
    <title>Retrieving a Textbox Value Example</title>
  </head>
  <body> 
    <textarea rows="5" cols="25" name="txt2"></textarea> <br /> <textarea rows="5"  cols="25" name="txt3"></textarea>
    <br />
    <input type="button" value="Set Values" onclick="setValues()" />

    <script type="text/javascript">
      function setValues() { 
      var oTextbox2= document.getElementById("txt2");
      oTextbox2 = oTextbox2.value; oTextbox2 = oTextbox2.split(" ");
      oTextbox2 = oTextbox2.sort();

      var oTextbox3 = document.getElementById("txt3"); 
      oTextbox3.value = oTextbox2;
      } 
    </script>

  </body>
</html>

You use document.getElementById but assign name s to your DOM elements. 您使用document.getElementById但将name s分配给DOM元素。 Use id attributes instead: 请改用id属性:

<textarea rows="5" cols="25" id="txt2"></textarea> <br /> <textarea rows="5"  cols="25" id="txt3"></textarea>

document.getElementById(“txt3”)将通过“ID”获取元素,你使用name =“txt3”你应该在你的textarea中使用id =“txt3”

Did you mean oTextbox3.value = oTextbox2.value ? 你的意思是oTextbox3.value = oTextbox2.value

And if you're using getElementById , then you need to use id , not name . 如果你正在使用getElementById ,那么你需要使用id ,而不是name

EDIT: Or use document.getElementsByName : 编辑:或使用document.getElementsByName

var oTextbox2 = document.getElementsByName('txt2')[0];

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

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