组验证错误后,文本框的位置正在发生变化

时间:2013-10-11 08:07:19

标签: jquery css css3 jquery-validate

我使用了jquery验证的组验证来验证包含3个文本框的电话号码。我的代码将显示3个文本框的单个验证错误。但如果发生验证错误,则文本框位置会发生变化。以下是我的代码

<!DOCTYPE html>
<html>
<head>

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script type="text/javascript" src="jquery_validation.js"></script>
    <style>
        .error{
            color: hsl(0, 100%, 50%);
            float: left;
            font: 10px/17px "Lato Reg",arial;
            width: 50%;
        }
        .sm_input{width:20%!important;margin-right: 3%;}
        .mid_input{width:36%!important;}
    </style>
    <script>
        $(document).ready(function() {


                $("#form1").validate({
                    groups: {
                        phoneNumber: "phone_no_1 phone_no_2 phone_no_3"
                    },
                    rules: {
                        'phone_no_1': {
                            required: true,
                            minlength: 3,
                            maxlength: 3,
                            number: true
                        },
                        'phone_no_2': {
                            required: true,
                            minlength: 3,
                            maxlength: 3,
                            number: true
                        },
                        'phone_no_3': {
                            required: true,
                            minlength: 4,
                            maxlength: 4,
                            number: true
                        }
                    },
                    errorPlacement: function(error, element) {
                        if (element.attr("name") == "phone_no_1"
                                || element.attr("name") == "phone_no_2" || element.attr("name") == "phone_no_3")
                            error.insertAfter("#phone_no");
                        else
                            error.insertAfter(element);
                    }

            });
        });
    </script>
</head>
<body>
    <form id="form1">

        <label>Phone Number</label>

        <input  type="text" class="sm_input" name="phone_no_1" id="phone_no_1" maxlength="3" size="3" tabIndex=1> <input  type="text" class="sm_input" name="phone_no_2" id="phone_no_2" maxlength="3" size="3" tabindex=1 >   <input  type="text" class="mid_input" name="phone_no_3" id="phone_no_3" maxlength="4" size="4" tabindex=3 > 
        <label id="phone_no"></label>
        <div class="clear"></div>




    </form>

</body>
</html>

发生错误时会发生这种情况enter image description here

1 个答案:

答案 0 :(得分:1)

here是我认为适合您的问题的解决方案。

you just have to float text and labels boxes using float:left;

 <label style="float:left;">Phone Number</label>

也将float: left添加到输入类。

  .sm_input{width:20%!important;margin-right: 3%;float:left;}

   .mid_input{width:36%!important;float:left;}