简体   繁体   English

计算弹出文本框中的字符

[英]Counting characters in a popup textbox

What I need to do is give one of those character counters to let users know how many characters they can enter into a textbox. 我需要做的是给这些字符计数器之一,以让用户知道他们可以在文本框中输入多少个字符。

The trick, apparently, is that the textbox is on a popup box. 显然,诀窍在于文本框位于弹出框上。

I tried implementing the code found here , but it doesn't seem to be working. 我尝试实现此处找到的代码,但似乎无法正常工作。 No errors or anything, it's just that the number of characters left never changes and I can type well past the supposed limit. 没有错误或其他任何东西,只是剩下的字符数没有变化,而且我可以输入超出预期限制的字符。

What I have is this: 我所拥有的是:

<!--//**********************************
    // Comment Character Count
    //**********************************  -->
<script type="text/javascript">
     function textCounter(field, countfield, maxlimit) {
     if (field.value.length > maxlimit)
        field.value = field.value.substring(0, maxlimit);
     else
        countfield.value = maxlimit - field.value.length;
     }
 </script>

And then down in my code body I have this: 然后在我的代码主体中,我有以下内容:

    <asp:Panel ID="pnl" runat="server" Height="190px" BackColor="#0f6fc6" ScrollBars="Auto">
        <asp:TreeView ID="TreeView1" runat="server" ShowLines="true" PopulateNodesFromClient="false" OnSelectedNodeChanged="TreeView1_PopupCommentsBox" Height="118px" ShowExpandCollapse="true" Font-Size="X-Small" Width="295px" NodeIndent="10" Font-Bold="True" ForeColor="White" ExpandDepth="0"></asp:TreeView>   
        <asp:TextBox ID="txtTreeselect" runat="server" Enabled="False" Visible="False"></asp:TextBox></asp:Panel>
<!--  ************************************  -->
            <asp:modalpopupextender id="MdlCommentsExtender" runat="server"     
                targetcontrolid="TreeView1" popupcontrolid="pnlComments" 
                popupdraghandlecontrolid="PopupHeader" drag="True" 
                backgroundcssclass="ModalPopupBG" Enabled="False"  >
            </asp:modalpopupextender>

            <asp:panel id="pnlComments" style="display: none" runat="server" BackColor="White" CssClass="modalPopup">
                <div class="HellowWorldPopup">
                    <div class="PopupHeader" id="Div3" style="border: thin solid #000000; vertical-align: middle; text-align: center; background-color: #C0C0C0; color: #000000; font-weight: bold; height: 40px;" ><br />Pend Comment</div>
                        <div class="PopupBody" style="background-color: #FFFFFF">
                            <table style="width: 300px">
                                <tr style="text-align:left">
                                    <td style="padding:4px"><asp:Label ID="lblCommentBox" runat="server" Text="Comment:"></asp:Label></td>
                                </tr>
                                <tr>
                                    <td style="padding:4px">
                                   <!--     <asp:TextBox ID="txtCommentBox_Old" runat="server" CssClass="textbox" TextMode="multiline" Wrap="True" Height="70px" Width="270px" Font-Size="Small"></asp:TextBox>  -->
                                        <asp:TextBox ID="txtCommentBox" TextMode="MultiLine" CssClass="textbox" Wrap="True" Height="70px" Width="270px" Font-Size="Small" Rows="3" runat="server" onkeyup="textCounter(txtCommentBox, this.form.remLen, 50);" onkeydown="textCounter(txtCommentBox, this.form.remLen, 50);" />
                                    </td>
                                </tr>  
                                <tr>
                                    <td>
                                        <input readonly="readonly" type="text" id="remLen" name="remLen" size="2" maxlength="3" value="50" /> characters left
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="Controls">
                            <table style="width: 300px">
                                <tr>
                                   <td style="vertical-align: middle; text-align: center"> <asp:Button ID="mdlCmntsOk_Click" runat="server" Text="OK" CssClass="textbox" Height="28px" Width="75px" OnClick="mdlCommentsOk_Click" /></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>  
            </asp:panel>
<!--  ************************************  -->

Can anyone tell me what might be going wrong? 谁能告诉我可能出了什么问题? Is this not working because it's inside a modalpopupextender? 这是不起作用的,因为它位于modalpopupextender内部吗?

Change the event attributes to this: 将事件属性更改为此:

onkeyup="textCounter( this, this.form.remLen, 50);" 
onkeydown="textCounter( this, this.form.remLen, 50);"

You were using a variable name that had no value in that scope. 您使用的变量名在该范围内没有任何值。 this in an event attribute always refers to the owning node. this在事件属性中始终指的是所属节点。 If you had defined txtCommentBox globally in JavaScript (either in the head or document), then it would be useable there. 如果您已在JavaScript中(在txtCommentBox或文档中)全局定义了txtCommentBox ,则在那里可以使用它。

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

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