做一个输入英文或汉字 的同时显示输入字的个数 到底是那里的问题了
<html>
<head>
<title>text</title>
<script>
var regChi=/[\u4e00-\u9fa5]/g;
var strInput=document.getElementById('tbContent').value;
var f=regChi.text(strInput);
function check();
{
f?check2():check1(Input);
} function check1(strInput)
{
var num=null;
var reg = /\b/g;
while(reg.exec(strInput))
num++
document.getElementById('txt').value=num/2;
}
function check2()
{
document.getElementById('txt').value=strInput.length;
}
</script>
</head>
<body >
<textarea cols="15" rows="25" onpropertychange="check()"> </textarea>
<input type="text" value="" id="txt">
</body>
</html>
<html>
<head>
<title>text</title>
<script>
var regChi=/[\u4e00-\u9fa5]/g;
var strInput=document.getElementById('tbContent').value;
var f=regChi.text(strInput);
function check();
{
f?check2():check1(Input);
} function check1(strInput)
{
var num=null;
var reg = /\b/g;
while(reg.exec(strInput))
num++
document.getElementById('txt').value=num/2;
}
function check2()
{
document.getElementById('txt').value=strInput.length;
}
</script>
</head>
<body >
<textarea cols="15" rows="25" onpropertychange="check()"> </textarea>
<input type="text" value="" id="txt">
</body>
</html>
<html>
<head>
<title>text </title>
<script type="text/javascript">
String.prototype.realLength = function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
}
function check()
{
strInput=document.getElementById('tbContent').value;
var l = strInput.realLength();
document.getElementById("txt").value = l; } </script>
</head>
<body >
<textarea cols="15" rows="25" onkeyup="check()" id="tbContent"></textarea>
<input type="text" value="" id="txt">
</body>
</html>
--------------------------------------------
你的程序逻辑上就有问题。使用的方法也不对,而且甚至有的地方拼写也错了,把{写成【等等。
如果你想自己学习用,那么上面的代码就可以了,如果你想放到工程里面实际用,可还要考虑onpress问题,因为以前我也做过
这个需求的东西。不然会有问题,你如果需要支持onpress和onkeyup的例子,可以给我发邮件:[email protected]
我可以回给你忠言逆耳,麽怪!