要跨越的字符数的多重计数?
我尝试向表单添加两个单独的最大字符字段失败。我尝试重命名元素 id 并更改 var i 和 c 但无济于事。谢谢
小提琴 http://jsfiddle.net/rLkcy0t4/
这是代码。
var maxchar = 160;
var i = document.getElementById("textinput");
var c = document.getElementById("count");
c.innerHTML = maxchar;
i.addEventListener("keydown", count);
function count(e) {
var len = i.value.length;
if (len >= maxchar) {
e.preventDefault();
} else {
c.innerHTML = maxchar - len - 1;
}
}
textarea {
display: block;
width: 200 px;
height: 100 px;
}
Remaining characters: <span id="count"></span>
<textarea id="textinput">
</textarea>
I have been unsuccessful in trying to add two seperate maximum character fields to a form. I have tried renaming the element ids and changing the var i and c but to no avail. Thanks
Fiddle http://jsfiddle.net/rLkcy0t4/
Here is the code.
var maxchar = 160;
var i = document.getElementById("textinput");
var c = document.getElementById("count");
c.innerHTML = maxchar;
i.addEventListener("keydown", count);
function count(e) {
var len = i.value.length;
if (len >= maxchar) {
e.preventDefault();
} else {
c.innerHTML = maxchar - len - 1;
}
}
textarea {
display: block;
width: 200 px;
height: 100 px;
}
Remaining characters: <span id="count"></span>
<textarea id="textinput">
</textarea>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
委派和数据属性将有所帮助
Delegation and data attributes will help
你的小提琴中的问题是你声明了多个具有相同名称的变量。您声明
i
和c
两次。您还声明了函数count
两次。将其中一半变量重命名为唯一的名称,它应该按照您期望的方式工作。您可以在此 fiddle 中看到一个工作示例。
The problem in your fiddle is you are declaring multiple variable with the same name. You declare
i
andc
twice. You also declare the functioncount
twice. Rename half of those variables to something unique and it should work how you expect it to.You can see a working example in this fiddle.