简体   繁体   English

使用函数计算文本框字符

[英]Counting textbox characters using a function

So I have been trying to get this working for 2 days now and still no luck. 所以我一直在努力使它工作2天,但仍然没有运气。 I have a function that counts the characters of a textbox and display the results in a label. 我有一个计算文本框字符并在标签中显示结果的函数。

function textCounter2(field, counter, maxlimit) {

    if (field.value.length > maxlimit)
        field.value = field.value.substring(0, maxlimit);
    else
        document.getElementById('subtitlecount_lbl').innerText = maxlimit - field.value.length;
}

What I am trying to do is to use (input variable counter) inside getElementByID instead of manually typing subtitlecount_lbl. 我想做的是在getElementByID内使用(输入变量计数器),而不是手动键入subtitlecount_lbl。

I need to use the same function for about 15 textbox and labels. 我需要对大约15个文本框和标签使用相同的功能。

Its working fine. 它的工作正常。 Maybe it is a problem with the correct parameter handover. 正确的参数切换可能是一个问题。

 function textCounter2(field, counter, maxlimit) { if (field.value.length > maxlimit) { field.value = field.value.substring(0, maxlimit); } else { document.getElementById(counter).innerText = maxlimit - field.value.length; } } 
 <input name="test" onkeyup="textCounter2(this, 'subtitlecount_lbl', 5)" onchange="textCounter2(this, 'subtitlecount_lbl', 5)" /><br /> <span id="subtitlecount_lbl"></span> characters left 

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

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