简体   繁体   English

只允许选中2个复选框

[英]Only allow 2 checkboxes checked

I wanna say i rarely work with javascript. 我想说我很少使用javascript。 It's never been my thing and it probably never will be, so i'm sorry for my lack of knowledge to this language.. Which is why my question is so humiliating. 这从来都不是我的事,而且可能永远也不会。所以我很抱歉我对这种语言缺乏了解。这就是为什么我的问题如此令人羞辱。

I found this code online: 我在网上找到此代码:

function checkboxlimit(checkgroup, limit){
    var checkgroup=checkgroup
    var limit=limit
    for (var i=0; i<checkgroup.length; i++){
        checkgroup[i].onclick=function(){
        var checkedcount=0
        for (var i=0; i<checkgroup.length; i++)
            checkedcount+=(checkgroup[i].checked)? 1 : 0
        if (checkedcount>limit){
            alert("You can check a maximum of "+limit+" boxes.")
            this.checked=false
            }
        }
    }
}

<script type="text/javascript">
    checkboxlimit(document.forms.checkform.weapon[], 2)
</script>

And it looks pretty basic to me. 对我来说,这看起来很基础。 Nothing speciel here. 这里没什么特别的。 But, my problem is that i'm trying to find the checkbox through the name weapon[], which i'm required to as i'm outputting multiple checkboxes as weapon[] for array's. 但是,我的问题是我正在尝试通过名称武器[]查找复选框,因为我要输出多个复选框作为阵列的武器[],所以我需要使用该名称。 Now my question is, how i'll make this work with this stupid little error. 现在我的问题是,我将如何解决这个愚蠢的小错误。

To sum up: My problem is that my checkboxes are named weapon[] (name="weapon[]") instead of (name="weapon"), as an array, and therefor the js wont recognize it. 总结一下:我的问题是,我的复选框以数组的形式而不是(name =“ weapon”)的名称被命名为武器[](name =“ weapon []”),因此js无法识别它。

Are you looking for something like this? 您是否正在寻找这样的东西?

$(':checkbox[name=weapon]').on('click',function(){

    var checkedBoxlength=$(':checkbox[name=weapon]:checked').length;
    if(checkedBoxlength>2){
        alert('You can check a maximum of 2 boxes.');
        return false;
    }
});

http://jsfiddle.net/umqL3/2/ http://jsfiddle.net/umqL3/2/

--- Edit -编辑

Just made a jquery plugin just for limiting how many chekboxes you check. 刚刚制作了一个jquery插件,仅用于限制您检查的复选框数量。

(function( $ ){

  $.fn.LimitCheckableNumber = function( options ) {     
    var settings = $.extend( {
      'nr'         : 1,    
    }, options);
    return this.each(function() {        
         var $this=$(this),
             thisName=$this.attr('name');
        $this.on('click',function(){            
            var count=$(":checked[name='"+thisName+"']").length;           
            if(count>settings.nr){
            alert('You can check a maximum of '+settings.nr+' boxes. ');
                return false;
            };            
        });
    });
  };
})( jQuery );

Link 链接

You can't use the [] when using dot notation. 使用点表示法时,不能使用[] Switch to treating it like an associative array. 切换为将其视为关联数组。

checkboxlimit(document.forms.checkform['weapon[]'], 2)

http://jsfiddle.net/4mYbD/3/ http://jsfiddle.net/4mYbD/3/

Works just fine. 效果很好。

Also, you don't need to redefine checkgroup and limit . 另外,您不需要重新定义checkgrouplimit You can just use them as the arguments. 您可以将它们用作参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM