JavaScript 逗号计数器

发布于 2024-12-08 10:13:08 字数 821 浏览 0 评论 0原文

<HTML>
  <HEAD>
  <TITLE>Test Input</TITLE>
  <SCRIPT LANGUAGE="JavaScript">

  function validate (form) {
  var TestVar = form.inputbox.value;
  myArray = TestVar.split(',');
document.write(myArray.length);

  }
  </SCRIPT>
  </HEAD>
  <BODY>
  <FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
  <INPUT TYPE="text" NAME="inputbox" VALUE="" OnBlur="validate(this.form)" onkeydown="validate(this.form)" onkeyup="validate(this.form)" ><P>

<script type="text/javascript">validate(this.form);</script>
  </FORM>
  </BODY>
  </HTML>

有人可以帮我解决这个问题吗?只要我按下一个键,它就结束。 我希望它无限期更新。

例子:

输入:你好
输出:1
输入你好,很好,再见
输出:3
...

<HTML>
  <HEAD>
  <TITLE>Test Input</TITLE>
  <SCRIPT LANGUAGE="JavaScript">

  function validate (form) {
  var TestVar = form.inputbox.value;
  myArray = TestVar.split(',');
document.write(myArray.length);

  }
  </SCRIPT>
  </HEAD>
  <BODY>
  <FORM NAME="myform" ACTION="" METHOD="GET">Enter something in the box: <BR>
  <INPUT TYPE="text" NAME="inputbox" VALUE="" OnBlur="validate(this.form)" onkeydown="validate(this.form)" onkeyup="validate(this.form)" ><P>

<script type="text/javascript">validate(this.form);</script>
  </FORM>
  </BODY>
  </HTML>

Can someone please help me fix this. As soon as I press a key down, it ends.
I want it to update indefinitely.

Example:

input: hello
output: 1
input hello, good, bye
output: 3
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2024-12-15 10:13:08

当您在加载文档后执行 document.write() 时,它会清除当前文档并开始写入新文档。加载文档后请勿使用 document.write(),除非在极少数情况下您想要这种效果。这不是你想要的。

如果您只是想查看 myArray.length 值用于调试目的,请使用 console.log() 而不是 document.write() 并查看它在控制台/调试窗口中。

更好的是使用 JavaScript 调试器之一(Firefox 的 Firebug 或 Chrome、Opera 和 Safari 中的内置调试器)并在验证函数中设置断点,然后在变量发生时检查变量。

When you do document.write() after your document has been loaded, it clears your current document and starts writing a new one. Don't use document.write() after your document has been loaded except in rare cases where that's the effect you want. That is not what you want here.

If you're just trying to see what the myArray.length value is for debugging purposes, then use console.log() instead of document.write() and look at it in the console/debug window.

Even better would be to use one of the javascript debuggers (Firebug for Firefox or the built-in debugger in Chrome, Opera and Safari) and set a breakpoint in your validate function and just inspect the variable as it happens.

不醒的梦 2024-12-15 10:13:08
<HTML>
 <HEAD>
 <TITLE>Test Input</TITLE>
</HEAD>
<BODY>
<form name="myForm">
<input type="text" id="inputbox" onkeydown="validate(form.this)"/>
<input type="text" name="textf"/>
</FORM>
 <SCRIPT LANGUAGE="JavaScript">
 function validate () {
 var TestVar = myForm.inputbox.value;
 var myArray = TestVar.split(',');
document.myForm.textf.value = myArray.length;

 }
</SCRIPT>
</BODY>
</HTML>

那很容易。

<HTML>
 <HEAD>
 <TITLE>Test Input</TITLE>
</HEAD>
<BODY>
<form name="myForm">
<input type="text" id="inputbox" onkeydown="validate(form.this)"/>
<input type="text" name="textf"/>
</FORM>
 <SCRIPT LANGUAGE="JavaScript">
 function validate () {
 var TestVar = myForm.inputbox.value;
 var myArray = TestVar.split(',');
document.myForm.textf.value = myArray.length;

 }
</SCRIPT>
</BODY>
</HTML>

That was easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文