在表单中限制选中的复选框 [英] Limit checked checkbox in a form

查看:94
本文介绍了在表单中限制选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一组复选框的HTML表单,如何让用户只能检查一个固定数量的表单

解决方案

p>这个例子将检查每个检查后的检查输入的数量,并与允许的最大数量进行比较。如果超过上限,则其余复选框将被禁用。

  jQuery(function(){
var max = 3 ;
var checkboxes = $('input [type =checkbox]');

checkboxes.change(function(){
var current = checkboxes.filter(' :'checked')。length;
checkboxes.filter(':not(:checked)')。prop('disabled',current> = max);
});
} );

下面是一个工作示例 - http://jsfiddle.net/jaredhoyt/Ghtbu/1/


I have an HTML form with a set of checkboxes, how to make the user can only check a fixed number of them

解决方案

This example will count the number of checked inputs after each one is checked and compare against the maximum number allowed. If the maximum is exceeded, the remaining checkboxes are disabled.

jQuery(function(){
    var max = 3;
    var checkboxes = $('input[type="checkbox"]');

    checkboxes.change(function(){
        var current = checkboxes.filter(':checked').length;
        checkboxes.filter(':not(:checked)').prop('disabled', current >= max);
    });
});

Here is a working example - http://jsfiddle.net/jaredhoyt/Ghtbu/1/

这篇关于在表单中限制选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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