如何报告用户输入的字符串长度

时间:2013-11-16 18:23:19

标签: javascript

好的,所以这是我的脚本......我不能让它输出字符串的长度!请帮助,当谈到Javascript时,我是一个完整的菜鸟,这是一个大学课程。谢谢!

<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">
/*
Purpose of script: report length of string given by user
*/
  "use strict";
  function stringLength()
  {
  var str=(document.StringLength.string);
  var n=str.length;
  document.StringLength.answerBox.value=length;
  }
  //Get string from user
  //Output to user
  </script>
</HEAD>
<BODY>
<form name = StringLength>
Enter Your String <input type = "text" name = "string">
<br>
Length <input type = "text" name = "answerBox">

<input type = "button" onclick = "stringLength()" value = "Run">


  </BODY>
</HTML>

4 个答案:

答案 0 :(得分:0)

您需要检查string输入框的的长度

function stringLength(){
    var str=document.StringLength.string.value;
    var len=str.length;
    document.StringLength.answerBox.value=len;
}

有关演示,请参阅此JSFiddle

答案 1 :(得分:0)

您可以尝试这样:

var x = document.getElementById('yourinput');
var y = document.getElementById('characters');
x.onkeyup = function() {
  y.innerHTML = x.value.length;
}

JSFIDDLE

答案 2 :(得分:0)

function stringLength()
  {
  var str=(document.StringLength.string);
  var n=str.length;
  document.StringLength.answerBox.value = n ;
  }

答案 3 :(得分:0)

你能试试这个吗,你还没有正确地结束form标记

<HTML>
<HEAD>
<TITLE></TITLE>
<script type="text/javascript">
    /*
    Purpose of script: report length of string given by user
    */

      function stringLength()
      {          
          document.StringLength.answerBox.value=document.StringLength.string.value.length;
      }
  //Get string from user
  //Output to user
  </script>
</HEAD>
    <BODY>
        <form name ="StringLength" >
            Enter Your String <input type = "text" name = "string">
            <br>        Length <input type = "text" name = "answerBox">     
            <input type = "button" onclick = "stringLength()" value = "Run">
        </form>/*encloded form tag here*/

     </BODY>
</HTML>