jQuery-如果>如何显示消息选中1个复选框? [英] jQuery - how to show message if > 1 checkbox selected?

查看:59
本文介绍了jQuery-如果>如何显示消息选中1个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据用户记录中的汽车数量n,将有n个复选框可供选择.

Depending on the number n of cars a user has in his records, there will be n checkboxes to select from.

在我看来,这些复选框是使用循环呈现的

In my view these checkboxes are rendered using a loop

<?php 
foreach ($cars as $row):
echo form_checkbox('cars_id[]', $row->cars_id, FALSE, 'class="checkbox"') . 
<span>'. $row->cars_name . '</span><br />';
endforeach; 
?>

如果用户选择> 1复选框,我想显示一条消息---:

I want to show a message if the user selects > 1 checkbox --- so:

[x] Honda
[ ] Toyota
[ ] Suzuki

//no message shown


[x] Honda
[X] Toyota
[ ] Suzuki

//message is shown

我尝试使用

if ($(".checkbox").is(':checked')) { etc...

有人建议如何使它工作吗?

Does anyone have suggestions how to get this to work?

非常感谢您的帮助.

推荐答案

如果已选中> 1个复选框,如何显示消息?

How to show message if > 1 checkbox selected?

您可以选中 :checked 复选框的length属性.

if ($('.checkbox:checked').length > 1) { 
   // Show message.
};

jsFiddle .

此外,您还可以使用 :checkbox 选择器选择这些复选框.

Also, you could select those checkboxes with :checkbox selector.

这篇关于jQuery-如果&gt;如何显示消息选中1个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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