Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery:选择任何复选框,但使用';这';并过滤到指定的一个_Jquery_Checkbox - Fatal编程技术网

jquery:选择任何复选框,但使用';这';并过滤到指定的一个

jquery:选择任何复选框,但使用';这';并过滤到指定的一个,jquery,checkbox,Jquery,Checkbox,目标:一次只允许选中4个复选框中的2个。我在这个函数中看到了我的问题: $(function () { var limit = 2; $("input:checkbox").change(function(){ var clicked = this; var nCurrentlyChecked = $("input:checkbox:checked").length; if ($(this).is(":checked")) {

目标:一次只允许选中4个复选框中的2个。我在这个函数中看到了我的问题:

$(function () {
    var limit = 2;
    $("input:checkbox").change(function(){
        var clicked = this;
        var nCurrentlyChecked = $("input:checkbox:checked").length;
        if ($(this).is(":checked")) {
            if (nCurrentlyChecked > limit)
                alert('too many. after 'ok' should deselect the checkbox clicked');
            $(this).prop('checked', false);
        } else {
                alert("good, should be able to select");
            $(this).prop('checked', true);
        }
    });
$("input:checkbox").change(function () {
    $(this).prop('checked', function (_, currentState) {
        return $("input:checkbox:checked").length > limit 
               ? !! alert('an error') 
               : currentState
    });
});
问题是尽量做到足够通用,这样我就不会编写多个函数,它们都做相同的事情。通过选择带有
$(“输入:检查”)
的“任意”复选框,并在两个结果/操作中使用
this
,函数将根据单击框时的状态,选中/取消选中所有复选框

现在,这个语句:
$(this.prop('checked',false)单击警报框中的“确定”后,将阻止复选框实际被选中…它将进行检查、发出警报,然后求助于其以前的状态,但它会从第一点开始执行此操作

与其他语句和警报类似

因此,我想知道是否有一种方式可以说,“嘿,只需取消选中调用此函数的复选框,忽略页面上的其他复选框”


没有为每个复选框在更具体的选择器下创建函数?

您的问题在于在
if
语句中正确分组。只有当选中的数量过多时,才应取消选中该框

$(function () {
    var limit = 2;
    $("input:checkbox").change(function () {
        var clicked = this;
        var nCurrentlyChecked = $("input:checkbox:checked").length;
        if ($(this).is(":checked")) {
            if (nCurrentlyChecked > limit) {
                alert('too many. after "ok" should deselect the checkbox clicked');
                $(this).prop('checked', false);
            }
        } else {
            alert("good, should be able to select");
        }
    });
});


您不应该在
else
子句中使用
$(this).prop('checked',true)
,因为当用户取消选中某个框而不是选中某个框时,会运行该操作。

看起来您的逻辑有点混乱。我会试着这样写:

$(function () {
var limit = 2;
$("input:checkbox").change(function(){
    var clicked = this;
    var nCurrentlyChecked = $("input:checkbox:checked").length;
    if(nCurrentlyChecked > limit && $(this).is(":checked")){
        $(this).prop('checked', false); // undo checked
    }
});

这将优先检查是否已达到限制,而不是检查当前元素,如果未达到限制,则与此无关。

您可以使用
.prop()
回调函数:

$(function () {
    var limit = 2;
    $("input:checkbox").change(function(){
        var clicked = this;
        var nCurrentlyChecked = $("input:checkbox:checked").length;
        if ($(this).is(":checked")) {
            if (nCurrentlyChecked > limit)
                alert('too many. after 'ok' should deselect the checkbox clicked');
            $(this).prop('checked', false);
        } else {
                alert("good, should be able to select");
            $(this).prop('checked', true);
        }
    });
$("input:checkbox").change(function () {
    $(this).prop('checked', function (_, currentState) {
        return $("input:checkbox:checked").length > limit 
               ? !! alert('an error') 
               : currentState
    });
});