如果未选中所有复选框,则警告用户 [英] warn user if all checkboxes are unchecked

查看:86
本文介绍了如果未选中所有复选框,则警告用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一系列复选框的表格.如果未选中所有复选框,我想警告用户,请点击提交".我正在使用以下代码来报告所有复选框的值:

I have a form with a series of checkboxes. I want to warn the user, after they hit submit, if ALL of the checkboxes are unchecked. I'm using the following code to report all of the values of the checkboxes:

$('[id^=leg_rider]').filter(':checked');

这似乎有效.但是,当我尝试检查返回的对象是否为空时,它似乎不起作用.这就是我正在尝试的:

This seems to work. However, when I try to check to see if the returned object is empty, it doesn't seem to work. This is what I'm trying:

        $("#set_pref_submit").click(function() {
        var legchecked = $('[id^=leg_rider]').filter(':checked');
        if (!legchecked){ alert("no riders")};
    });

任何建议都值得赞赏.谢谢!

Any suggestions are appreciated. Thanks!

推荐答案

您可以使用jQuery对象的 length 属性:

You can use jQuery object's length property:

$("#set_pref_submit").click(function() {
    var legchecked = $('input[id^=leg_rider]:checked').length;
    if (legchecked){ alert("no riders")};
});

这篇关于如果未选中所有复选框,则警告用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆