嗨,以下是我的登录表单,因为我想限制用户在用户名字段中输入 8 个字符,但它不起作用以下是我的代码:
<div data-role="content">
        <form id="loginForm">
            <div data-role="fieldcontain" class="ui-field-contain ui-body ui-br">
                <label for="username">Username:</label> <input type="text"
                    name="username" id="username" value=""   maxlength="8" /> <label for="password">Password:</label>
                <input type="password" name="password" id="password" value="" /> <label
                    for="dob">Date of birth:</label> <input type="password"
                    type="password" name="dob" id="dob" value="" />
            </div>
            <div >
                <fieldset class="ui-grid-a" >
                    <div class="ui-block-a" >
                        <input type="submit" data-role="button" value="Login"
                            id="submitButton">
                    </div>
                    <div class="ui-block-b">
                        <input type="reset" data-role="button" value="Cancel"
                            id="cancelButton">
                    </div>
                </fieldset>
            </div>
        </form>
    </div>
    <script type="text/javascript">
    </script>
    <script type="text/javascript">
        $("#username").live('keydown', function (event)
        {
            if ((event.keyCode.length>8))
            {
                event.preventDefault();
            }
        }); 
    </script>
但用户仍然可以在 UserName 字段中输入任意数量的字符。这段代码有什么问题?任何建议将不胜感激。提前致谢