文字長度測試

<script type="text/javascript">
    function CountStrLen(obj1, obj2) {
        var str = obj1.value;
        obj2.value = str.length;
    }
</script>

    <input id="txt1" onkeyup="CountStrLen(this,txt2)" type="text" />
    <input id="txt2" type="text" />

-----------------------

    <asp:textbox id="TextBox1" runat="server"></asp:textbox>
    <asp:textbox id="TextBox2" runat="server"></asp:textbox>

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        TextBox1.Attributes.Add("onkeyup", "CountStrLen(" &amp; TextBox1.ClientID &amp; "," &amp; TextBox2.ClientID &amp; ")")
        TextBox2.ReadOnly = True
    End Sub

-----------------------

如果要接收長度不是 textbox 而是 label 或 span 等等有 ID 的 html tag
要用 innerHTML

   txtState.Attributes.Add("onkeyup", "CountStrLen1(" & txtState.ClientID & "," & Label1.ClientID & ")")

   function CountStrLen1(obj1, obj2) {
        var str = obj1.value;
        obj2.innerHTML = str.length;
    }

沒有留言:

張貼留言