jquery验证无效。它没有验证

时间:2012-02-10 12:36:08

标签: javascript jquery jquery-validate

下面是JSP页面的附加源代码。我不知道为什么我的验证框架不起作用?

Can Any Body可以帮助我解决以前的经验。

每当我尝试点击提交按钮而不点击选择单选按钮时。它应该给我验证问题。但它没有用。

我的剧本中出了什么问题?

你能检讨一下并帮助我吗?

HTML代码:

<form method="post" id="identityVerification">
                <div class="error-message" id="error-message1" style="display:none;">
                    <p id="err_cars" style="font-size:13px;"></p>
                    <p id="err_street" style="font-size:13px;"></p>
                </div><br/>
                <div>
                    <p style="padding-bottom:15px; font-size:13px;">Which of the following car is registered under your name in state of Washington?</p>
                                <input type="radio" name="questions[0].answer" value="Ford Focus" id="questions[0].answer_0"></input>
                                <label for="questions[0].answer_0">Ford Focus</label>
                                <input type="radio" name="questions[0].answer" value="Chevy Aveo" id="questions[0].answer_1"></input>
                                <label for="questions[0].answer_1">Chevy Aveo</label>
                                <div class="clear"></div>
                                <input type="radio" name="questions[0].answer" value="Toyota Celica" id="questions[0].answer_2"></input>
                                <label for="questions[0].answer_2">Toyota Celica</label>
                                <input type="radio" name="questions[0].answer" value="None of these" id="questions[0].answer_3"></input>
                                <label for="questions[0].answer_3">None of these</label>
                                <div class="clear"></div>
                </div>
                <br/><br/><br/><br/>

                <div>
                    <p style="padding-bottom:15px; font-size:13px;">Which of the following street are you currently residing or have previously resided?</p>
                                <input type="radio" name="questions[1].answer" value="1 Grape Vine" id="questions[1].answer_0"></input>
                                <label for="questions[1].answer_0">1 Grape Vine</label>
                                <input type="radio" name="questions[1].answer" value="23 Main Street" id="questions[1].answer_1"></input>
                                <label for="questions[1].answer_1">23 Main Street</label>
                                <div class="clear"></div>
                                <input type="radio" name="questions[1].answer" value="87 Market Street" id="questions[1].answer_2"></input>
                                <label for="questions[1].answer_2">87 Market Street</label>
                                <input type="radio" name="questions[1].answer" value="None of these" id="questions[1].answer_3"></input>
                                <label for="questions[1].answer_3">None of these</label>
                                <div class="clear"></div>
                </div>
                <br/><br/><br/><br/>
                <input type="submit" class="ButtonFirst" value="Submit" id="submitVerifyButton">
            </form>

JavaScript代码:

$(document).ready(function()
        {
            validationRules = {
                    "cars": {required:true},
                    "street": {required:true}

                },
            validationMessages = {
                    "cars": "Please select car registered under your name",
                    "street": "Please select your residential street"
                };
            $("#identityVerification").validate(
            {
                rules: validationRules,
                messages: validationMessages,
                highlight: function(element) {
                    var id = element.id,
                    errorElement = $("#err_" + id),
                    errorContainer = errorElement.closest("div.error-message");
                    $(element).addClass("error-message");
                    errorContainer.show();  
                },
                unhighlight: function(element) {
                    var id = element.id;
                    $(element).removeClass("error-message");
                },
                success: function(errorElement)
                {
                    var errorContainer = errorElement.closest("div.error-message");         
                    if(errorContainer.find("span:visible").length == 0)
                        errorContainer.hide();
                },
                errorElement: "span"

            });
        });

2 个答案:

答案 0 :(得分:1)

所以我测试了你的代码,我不得不说你的代码运行正常。你犯了一个小错误。看看你的HTML代码,你会发现这样的东西:

<input type="radio" name="questions[1].answer" value="1 Grape Vine" id="questions[1].answer_0"></input> 

输入字段的名称为问题1。答案问题[0] .answer 。在您的javascript代码中,您根据需要声明了名称为*street* and *cars*的广播组。所以它指向表单中未定义的字段。 Jquery不会因为未定义的错误消息而烦扰用户,因此您的脚本似乎无效。

如果您不相信我,请参阅here

答案 1 :(得分:0)

我猜你没有在你的布局/页面中包含jquery.validation.js文件。尝试包含js文件。如果您没有使用jquery.validation,那么您应该使用它进行客户端验证。结帐以下链接了解更多详情。 http://docs.jquery.com/Plugins/Validation