3

我正在为我的应用程序使用 struts2 框架。我还使用 jquery 验证来验证客户端的表单输入。但是他们俩都不太好相处。

我有我希望它存在的 UserBean 类。

我正在使用以下 jquery & struts 表单.. 它不验证表单

    <script type="text/javascript">
        $(document).ready(function(){
            var validator = $("#myform").validate({
                rules: {
                    username: "required",
                    password: "required"
                },
                messages: {
                    username: "Please enter your username",
                    password: "Please enter your password"
                }
            });
        });
    </script>

 <s:form method="POST" action="doLogin" name="myform" id="myform" cssClass="form" theme="simple">
            <s:fielderror theme="simple" >

                    <label class="control-label" for="username">User Name </label>

                        <s:textfield name="userBean.userName" label="USER NAME"/>
                        <s:param>userBean.userName</s:param>



                    <label class="control-label" for="username">Password  </label>


                        <s:password name="userBean.password" label="PASSWORD" />
                        <s:param>userBean.password</s:param>

                <s:submit name="Submit" value="Submit"/>
            </s:fielderror>
        </s:form>
4

1 回答 1

2

您输入的名称是userBean.userNameuserBean.password因此您的脚本声明必须与这些名称匹配才能工作:

<script type="text/javascript">
    $(document).ready(function(){
        var validator = $("#myform").validate({
            rules: {
                "userBean.userName": "required",
                "userBean.password": "required"
            },
            messages: {
                "userBean.userName": "Please enter your username",
                "userBean.password": "Please enter your password"
            }
        });
    });
</script>

根据jQuery Validation on field(s) with a prefix in the name property,您需要将名称放在引号中,因为它在名称中包含 a .

于 2012-08-11T03:31:15.497 回答