Char计数器在Chrome中运行但不在Firefox中运行

时间:2014-06-28 15:57:08

标签: javascript asp.net vb.net

我已经实现了一个可在Chrome中运行的字符计数器功能,但在Firefox中不起作用。我已经在我的VB.NET页面上找到了它。你能帮我理解为什么它在Chrome中运行但不适用于Firefox吗?

 <br>Other comments (max 250 characters):
<br>
<asp:TextBox ID="OtherComments" runat="server" MaxLength="250" 
    onblur="javascript:CharCounter(this.id);" 
    onkeyup="javascript:CharCounter(this.id);" style="height:50px; width:450px;" 
    TextMode="MultiLine"></asp:TextBox>
<br>
    <asp:Label ID="lblCounter" runat="server" Text="250 chars remaining"></asp:Label>
    <script language="javascript" type="text/javascript">

    function CharCounter(textId) {
        var totalchar = '<%= OtherComments.MaxLength %>';
        var txtbox = document.getElementById(textId);
        var lbl = document.getElementById('<% =lblCounter.ClientID %>');
        if (txtbox.value.length > totalchar) {
            txtbox.value = txtbox.value.substring(0, totalchar);
        }
        lbl.innerText = (totalchar - txtbox.value.length) + " chars remaining";
    }
 </script>

1 个答案:

答案 0 :(得分:0)

您正在将字符串与整数进行比较。这可能是问题所在。使用innerHTML而不是innerText也更好。某些浏览器不支持InnerText。 希望这可以解决您的问题。

function CharCounter(textId) {
        var totalchar = <%= OtherComments.MaxLength %>;
        var txtbox = document.getElementById(textId);
        var lbl = document.getElementById('<% =lblCounter.ClientID %>');
        if (txtbox.value.length > totalchar) {
            txtbox.value = txtbox.value.substring(0, totalchar);
        }
        lbl.innerHTML = (totalchar - txtbox.value.length) + " chars remaining";
}