jQuery 验证插件过早显示验证消息

发布于 2024-11-09 09:09:39 字数 2910 浏览 0 评论 0原文

我有一个搜索表单,其中根据用户在下拉列表中选择的内容显示不同组的复选框。我想使用 jQuery 验证插件来检查用户是否 (a) 在下拉列表中选择了某些内容,以及 (b) 至少选中了一个复选框。

我已经让它工作了,只是复选框的验证消息表现得很奇怪。用户可以毫无问题地在下拉列表中选择一个值,但当用户单击页面上的任何内容(甚至是空白区域)时,会显示复选框的验证错误消息。有谁知道可能是什么原因造成的?

这是我的验证插件的 JS 代码。我选择让所有验证消息显示在下拉列表旁边(名称='to'),而不是复选框旁边(都具有类='from_checkbox')。

$(document).ready(function(){
    // Method for verifying that at least one checkbox has been checked
    jQuery.validator.addMethod("atLeastOneChecked", function(value, element, params) { 
        return $('.from_checkbox:checked').length > 0; 
    }, jQuery.format("From?"));

    // Tell jQuery validation plugin to validate the search form with the proper rules
    $("#search_form").validate({
        rules: {
            'to': { 
                required: true, 
                atLeastOneChecked: true
            }
        }, 
        messages: {
            'to': {
                required: "To?"
            }
        }
    });
});

大部分 html 是由 php 生成的,但输出如下所示:

<form action="results.php" method="post" id="search_form">
    <table>
        <tr>
            <td>Destination</td>
            <td>
                <select name="to" onchange='changeFromDiv(options[selectedIndex].value)'>
                    <option disabled="disabled" selected value="">--select--</option>
                    <option value="1">Country 1</option>
                    <option value="2">Country 2</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" id="from_label">Which cities can you travel from?</td> 
        </tr>
        <tr>
            <td colspan="2">
                <div style="display:none" class="from_div" id="from_1">
                    <input name="from[3]" type="checkbox" value="3" class="from_checkbox" /> City 3<br />
                    <input name="from[4]" type="checkbox" value="4" class="from_checkbox" /> City 4<br />
                    <input name="from[8]" type="checkbox" value="8" class="from_checkbox" /> City 8<br />
                </div>
                <div style="display:none" class="from_div" id="from_2">
                    <input name="from[1]" type="checkbox" value="1" class="from_checkbox" /> City 1<br />
                    <input name="from[6]" type="checkbox" value="6" class="from_checkbox" /> City 6<br />
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" id="submit_button" value="Find best route" onclick="showLoading()" />
            </td>
        </tr>
    </table>
</form>

I have a search form where different groups of checkboxes are shown depending on what the user chooses in a drop-down. I want to use jQuery validation plugin to check that the user has (a) chosen something in the drop-down, and (b) checked at least one checkbox.

I've gotten this to work, except that the validation message for the checkboxes is behaving strangely. The user can choose a value in the drop-down without problems, but as the user clicks anything on the page (even an empty area), the validation error message for the checkboxes is displayed. Does anyone know what may be causing this?

Here is my JS code for the validation plugin. I chose to have the all validation messages show up next to the drop-down (which has name='to'), rather than next to the checkboxes (which all have class='from_checkbox').

$(document).ready(function(){
    // Method for verifying that at least one checkbox has been checked
    jQuery.validator.addMethod("atLeastOneChecked", function(value, element, params) { 
        return $('.from_checkbox:checked').length > 0; 
    }, jQuery.format("From?"));

    // Tell jQuery validation plugin to validate the search form with the proper rules
    $("#search_form").validate({
        rules: {
            'to': { 
                required: true, 
                atLeastOneChecked: true
            }
        }, 
        messages: {
            'to': {
                required: "To?"
            }
        }
    });
});

Much of the html is generated by php, but the output looks something like this:

<form action="results.php" method="post" id="search_form">
    <table>
        <tr>
            <td>Destination</td>
            <td>
                <select name="to" onchange='changeFromDiv(options[selectedIndex].value)'>
                    <option disabled="disabled" selected value="">--select--</option>
                    <option value="1">Country 1</option>
                    <option value="2">Country 2</option>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" id="from_label">Which cities can you travel from?</td> 
        </tr>
        <tr>
            <td colspan="2">
                <div style="display:none" class="from_div" id="from_1">
                    <input name="from[3]" type="checkbox" value="3" class="from_checkbox" /> City 3<br />
                    <input name="from[4]" type="checkbox" value="4" class="from_checkbox" /> City 4<br />
                    <input name="from[8]" type="checkbox" value="8" class="from_checkbox" /> City 8<br />
                </div>
                <div style="display:none" class="from_div" id="from_2">
                    <input name="from[1]" type="checkbox" value="1" class="from_checkbox" /> City 1<br />
                    <input name="from[6]" type="checkbox" value="6" class="from_checkbox" /> City 6<br />
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" id="submit_button" value="Find best route" onclick="showLoading()" />
            </td>
        </tr>
    </table>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

獨角戲 2024-11-16 09:09:39

当您初始化插件时,只需包含“onfocusout”选项的设置:

$("#search_form").validate({
    rules: {
        'to': { 
            required: true, 
            atLeastOneChecked: true
        }
    }, 
    messages: {
        'to': {
            required: "To?"
        }
    },
    onfocusout: false
});

这将告诉插件不要对所涉及元素的“模糊”事件执行验证检查。它将等到表单提交。还有一个“onkeyup”标志,它可能与您无关,因为您不使用文本输入。

When you initialize the plugin, just include a setting for the "onfocusout" option:

$("#search_form").validate({
    rules: {
        'to': { 
            required: true, 
            atLeastOneChecked: true
        }
    }, 
    messages: {
        'to': {
            required: "To?"
        }
    },
    onfocusout: false
});

That will tell the plugin not to perform validation checks on the "blur" events from elements involved. It'll wait until the form is submitted. There's also an "onkeyup" flag, which probably isn't relevant to you since you're not working with text inputs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文