0

我在使用 jQuery Validation 插件和 Internet Explorer 时遇到问题。基本上,在页面加载时会显示“请输入有效的电子邮件地址”错误消息,尽管所有验证都正常工作,但在用户输入任何数据之前出现是相当难看的。

如果我删除电子邮件类并只填写必填字段没有问题,但我希望它验证电子邮件地址。

我在 IE7/8/9 中遇到了这个问题

这是我的代码:

//Form Validation
    $(document).ready(function(){
        $("#rsvpForm").validate({
           rules: {
             // simple rule, converted to {required:true}
             attendance: "required",
             firstname_main: "required",
             lastname_main: "required",

             // compound rule
             email: {
               required: true,
               email: true
             },
             total_guests: {
              required: "#attendance:checked"
            },
             firstname_partner: {
              required: function(element) {
                return $("input:radio[name='total_guests']:checked").val() == '2' || $("input:radio[name='total_guests']:checked").val() == '3' || $("input:radio[name='total_guests']:checked").val() == '4'
              }
            },

             lastname_partner: {
              required: function(element) {
                return $("input:radio[name='total_guests']:checked").val() == '2' || $("input:radio[name='total_guests']:checked").val() == '3' || $("input:radio[name='total_guests']:checked").val() == '4'
              }
            },
             firstname_child1: {
              required: function(element) {
                return $("input:radio[name='total_guests']:checked").val() == '3' || $("input:radio[name='total_guests']:checked").val() == '4'
              }
            },
             lastname_child1: {
              required: function(element) {
                return $("input:radio[name='total_guests']:checked").val() == '3' || $("input:radio[name='total_guests']:checked").val() == '4'
              }
            },
             firstname_child2: {
              required: function(element) {
                return $("input:radio[name='total_guests']:checked").val() == '4'
              }
            },
             lastname_child2: {
              required: function(element) {
                return $("input:radio[name='total_guests']:checked").val() == '4'
              }
            },
           }
        });

        jQuery.extend(jQuery.validator.messages, {
            required: 'Required',
        });
      });
// Submit Handler
$(document).ready(function() { 
        $('#rsvpForm').ajaxForm({ 
            target:     '#content', 
            url:        'components/process-form-data.php' 

        }); 
    }); 

这些是我的标题链接:

    <script src="http://code.jquery.com/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script> 
<script type="text/javascript" src="../javascript.js"></script>

表单 HTML:

<form method="post" action="components/process-form-data.php" id="rsvpForm">
            <fieldset>
                <label for="attendance">Will you be attending the wedding? <em>*</em></label>
                <div class="radioBtns">
                <input class="signUp" type="radio" value="Yes" name="attendance" id="attendance" />
                <label class="signUp">Yes</label>

                <input class="signUp" type="radio" value="No" name="attendance" />
                <label class="signUp">No</label>
                </div>
            </fieldset>
            <h2>Main Guest</h2>
            <fieldset>
                <label for="firstname_main" class="standard">First name<em>*</em> :</label>

                <input type="text" name="firstname_main" id="firstname_main" placeholder="First name" />             
            </fieldset>             
            <fieldset>
                <label for="lastname_main" class="standard">Last name<em>*</em> :</label>
                <input type="text" name="lastname_main" id="lastname_main" placeholder="Last name" />
            </fieldset>
            <fieldset>
                <label for="email" class="standard">Email<em>*</em> :</label>
                <input type="text" name="email" id="email" placeholder="Email" />
            </fieldset>
            <div id="guests">   
            <fieldset class="wideField">
                <label for="total_guests">Will you be bringing a partner and/or young children? <em>*</em></label>
                <div class="radioBtns">
                <input class="signUp" type="radio" value="1" name="total_guests" id="total_guests" />
                <label class="signUp">No</label>
                <input class="signUp" type="radio" value="2" name="total_guests" />
                <label class="signUp">Just Partner</label>
                <input class="signUp" type="radio" value="3" name="total_guests" />
                <label class="signUp">Partner + 1 young child</label>
                <input class="signUp" type="radio" value="4" name="total_guests" />
                <label class="signUp">Partner + 2 young children</label>
                </div>
            </fieldset>
            </div>
            <div id="partnerDetails" style="display:none;">
            <h2>Partner Details</h2>
            <fieldset>
                <label for="firstname_partner" class="standard">First name<em>*</em> :</label>

                <input type="text" name="firstname_partner" id="firstname_partner" placeholder="First name" />             
            </fieldset>             
            <fieldset>
                <label for="lastname_partner" class="standard">Last name<em>*</em> :</label>
                <input type="text" name="lastname_partner" id="lastname_partner" placeholder="Last name" />
            </fieldset>
            </div>
            <div id="childDetails" style="display:none;">
            <h2>Child Details</h2>
            <fieldset>
                <label for="firstname_child1" class="standard">First name<em>*</em> :</label>

                <input type="text" name="firstname_child1" id="firstname_child1" placeholder="First name" />             
            </fieldset>             
            <fieldset>
                <label for="lastname_child1" class="standard">Last name<em>*</em> :</label>
                <input type="text" name="lastname_child1" id="lastname_child1" placeholder="Last name" />
            </fieldset>
            </div>
            <div id="secondChildDetails" style="display:none;">
            <fieldset>
                <label for="firstname_child2" class="standard">First name<em>*</em> :</label>

                <input type="text" name="firstname_child2" id="firstname_child2" placeholder="First name" />             
            </fieldset>             
            <fieldset>
                <label for="lastname_child2" class="standard">Last name<em>*</em> :</label>
                <input type="text" name="lastname_child2" id="lastname_child2" placeholder="Last name" />
            </fieldset>
            </div>
            <div id="diet">
            <fieldset class="wideField">
                <label for="dietaryRequirements">Do any of your guests have special dietary requirements :</label>
                <textarea cols="55" rows="8" name="dietaryRequirements" id="dietaryRequirements"></textarea>
            </fieldset>
            </div>
            <input id="submit_btn" input="" type="submit" value="Submit RSVP" class="btn" />            
        </form>
4

2 回答 2

0

我刚刚在 IE8 中尝试了您的表单,没有问题。如果您在 IE7/9 的初始页面加载中看到电子邮件字段错误消息,请尝试使用以前版本的插件: http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min。 js

希望能帮助到你!

于 2012-11-24T09:04:50.350 回答
0

感谢您的回复,但这似乎没有帮助,但我认为我已经隔离了问题。

我的页面中还有一些代码用于填充 IE 中的占位符文本。当我使用“电子邮件”作为占位符文本时,验证正在触发。

<script type="text/javascript">
        /* <![CDATA[ */
        $(function() {
            var input = document.createElement("input");
            if(('placeholder' in input)==false) { 
                $('[placeholder]').focus(function() {
                    var i = $(this);
                    if(i.val() == i.attr('placeholder')) {
                        i.val('').removeClass('placeholder');
                        if(i.hasClass('password')) {
                            i.removeClass('password');
                            this.type='password';
                        }           
                    }
                }).blur(function() {
                    var i = $(this);    
                    if(i.val() == '' || i.val() == i.attr('placeholder')) {
                        if(this.type=='password') {
                            i.addClass('password');
                            this.type='text';
                        }
                        i.addClass('placeholder').val(i.attr('placeholder'));
                    }
                }).blur().parents('form').submit(function() {
                    $(this).find('[placeholder]').each(function() {
                        var i = $(this);
                        if(i.val() == i.attr('placeholder'))
                            i.val('');
                    })
                });
            }
        });
        /* ]]> */
</script>

关于如何解决这个问题的任何建议?

于 2012-11-24T11:48:42.457 回答