简体   繁体   English

jQuery UI MultiSelect窗口小部件-无法取消选中复选框

[英]jQuery UI MultiSelect Widget - Can't uncheck checkbox

I'm using 2 widgets that adds the selections and only allows the user to select 5 total between both dropdown widgets. 我正在使用2个添加选择的小部件,并且只允许用户在两​​个下拉小部件中总共选择5个。 Strangely once the 5 limit is reached, I can not uncheck the checkboxes! 奇怪的是,一旦达到5个限制,我便无法取消选中该复选框! Anyone know why this is happening? 有人知道为什么会这样吗? I also noticed that if I say >5, the user can still choose 6 before is stopped. 我还注意到,如果我说> 5,则用户仍可以在停止之前选择6。 I'm having to use 4 to stop at 5? 我必须使用4停在5?

<select id="dropdown1" multiple="multiple" class="multiselect">
<select id="dropdown2" multiple="multiple" class="multiselect">

js js

$(document).ready(function() {
    $(".multiselect").multiselect({
    header: "Choose up to 5 areas",
    click: function(event,ui){
               if($(".multiselect").children(":checked").length > 4){               
                    return false;

        }},

      selectedList:5
    });

reference here for how I got to here: JS logic - adding 2 multiselect checkboxes 有关如何到达此处的信息,请参考此处: JS逻辑-添加2个多选复选框

UI widget i'm using: http://www.erichynds.com/blog/jquery-ui-multiselect-widget 我正在使用的UI小部件: http : //www.erichynds.com/blog/jquery-ui-multiselect-widget

Edit Let me start again as I've recently learned some knowledge that changed what I thought I knew. 编辑让我重新开始,因为我最近学到了一些知识,这些知识改变了我以为我知道的东西。

Why can't I uncheck checkboxes 为什么我不能取消选中复选框

You can't uncheck the checkboxes because you are preventing the default action by returning false from the click event handler if there are more than 4 boxes that are checked. 您不能取消选中复选框,因为如果选中的框超过4个,则您将从click事件处理程序中返回false来阻止默认操作。

if ($('.multiselect').children(':checked').length > 4)
    return false;

This if statement evaluates to true if more than 4 checkboxes are checked. 如果选中了四个以上的复选框,则此if语句的值为true。 Since you don't allow checkboxes to be checked or unchecked, that means once 4 have been checked, they will always be checked ( until page refresh or programmatically uncheck ). 由于您不允许选中或取消选中复选框,这意味着一旦选中了4,它们将始终处于选中状态( 直到页面刷新或以编程方式取消选中 )。

Why do I have to use 4 to stop at 5. 为什么我必须使用4停在5。

The reason for this isn't what I thought at first. 最初的原因不是我最初想的。 I found out that when you click a checkbox, it's state is changed before entering the click event handler. 我发现,当您单击复选框时,其状态在进入click事件处理程序之前已更改。 However, if you return false from the click handler, it's state will revert to what it was previously. 但是,如果您从点击处理程序中返回false,则其状态将恢复为之前的状态。

What this means is that if you have 4 checkboxes checked, and you click a 5th checkbox, before you see the UI update with the 5th checked checkbox, programmatically, 5 checkboxes are checked. 这意味着,如果您选中了4个复选框,然后单击了第5个复选框,那么在看到带有第5个选中复选框的UI更新之前,将以编程方式选中5个复选框。 If you return false, this number will drop back to 4. If you return true, this number would stay at 5. 如果返回false,则该数字将回落到4。如果返回true,则该数字将保持为5。


Solution

Here's the solution I came up with in a jsFiddle . 这是我在jsFiddle中提出的解决方案。

Notice. 注意。 I'm not using your multiselect plugin here, as I don't think this has anything to do with it. 我不在这里使用您的multiselect插件,因为我认为这与它无关。

$('.multi-select').click(function (e) {
    if (!this.checked) return true;

    if ($('.multi-select:checked').length > 3)
        return false;
});

This code will only allow you to select 3 checkboxes at a time. 此代码仅允许您一次选择3个复选框。

It checks if the clicked checkbox is not checked.. This means that the checkbox was previously checked. 它检查是否未选中单击的复选框。这意味着该复选框先前已被选中。 If the checkbox was checked previously, then we always want to allow the checkbox to be unchecked. 如果该复选框之前已被选中,那么我们总是希望取消选中该复选框。

After that, it checks how many checkboxes are checked. 之后,它将检查选中了多少个复选框。 I used 3 as the limit for how many checkboxes could be checked at a time. 我使用3作为一次可以检查多少个复选框的限制。 You could change (3) to (4) for your domain. 您可以将域的(3)更改为(4)。

Here's your domain specific code: 这是您的域特定代码:

$(document).ready(function() {
    $(".multiselect").multiselect({
        header: "Choose up to 5 areas",
        click: function (event, ui) {
            if (!this.checked) return true; //Allow previously checked checkboxes to be unchecked.

            if ($(".multiselect").children(":checked").length > 4) { //Only allow 4 checkboxes to be checked simultaneously.
                return false;
            }
        },
        selectedList:5
    });
});

You can do like this 你可以这样

$("select").multiselect({ 
      selectedList:5,
      header: "Choose only Five items items!",
 click: function(e){
   if( $(this).multiselect("widget").find("input:checked").length > 5 ){

       return false;
   } 
     }
  });

The OP (user3191137) is using Eric Hynds jQuery UI MultiSelect Widget drop down selector. OP(user3191137)使用Eric Hynds jQuery UI MultiSelect Widget下拉选择器。 The "click" method built in to the API assigns $(this) values specifically tied to the unique 'widget' id of each MultiSelect instance on the page. API内置的“ click”方法会分配$(this)值,这些值专门与页面上每个MultiSelect实例的唯一“小部件” ID关联。

This causes a problem trying to count the overall number of checkboxes across two or more selector instances on the page. 这会导致在尝试计算页面上两个或多个选择器实例之间的复选框总数时出现问题。 So a separate control is required which can keep track of the number of checkboxes checked overall. 因此,需要一个单独的控件来跟踪整个复选框的数量。

My jsFiddle addresses this problem. 我的jsFiddle解决了这个问题。

I have added a message layer into my example just to help show the status during the selections. 我在示例中添加了一个消息层,以帮助显示选择过程中的状态。

The way I solved this issue was having a separate variable counting the number of checks called "ClikCount". 我解决此问题的方法是使用一个单独的变量来计数称为“ ClikCount”的检查数量。 Checking this value either allows a selection or removes the selection if more than the max of 5 in this case. 在这种情况下,选中此值将允许选择,或者如果选择的最大值大于5,则将其删除。

HTML example: HTML示例:

<p class="message success">Check up to 5 checkboxes in total.</p>
<div class="box">
<select id="dropdown_1" multiple="multiple" class="multiselect">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    </select></div>
<div class="box">
<select id="dropdown_2" multiple="multiple" class="multiselect">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    </select></div>

CSS used: 使用的CSS:

.box{float:left;margin:10px 0;}
.multiselect{width:300px;}
.error { background:#FBE3E4; color:#8a1f11; border-color:#FBC2C4 }

Javascript for Message and Counter: 消息和计数器的Javascript:

var warning = $(".message");
    var ClikCount = 0;

Javascript to initialize all the Multi selectors: 初始化所有Multi选择器的Javascript:

$('.multiselect').multiselect({
                multiple: true,
                height: '175px',
                //header: 'Choose up to 5 areas',
                noneSelectedText: 'Choose up to 5 areas Total',
                selectedText: function(numChecked, numTotal, checkedItems){
                  return numChecked + ' of ' + numTotal + ' checked';
                },
                selectedList: false,
                show: ['blind', 200],
                hide: ['fade', 200],
                position: {
                    my: 'left top',
                    at: 'left bottom'
                }

            });

Javascript to Count/Control the clicks overall in Multi selectors: 用于计数/控制多重选择器中总体点击次数的Javascript:

(UnComment alerts to step through the process) (取消注释警报将逐步执行该过程)

$("input[type = 'checkbox']").change(function () {
                var chekt = ($(this).is(':checked'));
                //alert("Is this.checked =" + chekt);
                if (chekt == true) {
                    if (ClikCount <= 4) {
                        warning.addClass("success").removeClass("error").html("Check a few boxes.");
                        ClikCount = ClikCount + 1;
                        //alert("count = " + ClikCount);
                    }
                    else {
                        warning.addClass("error").removeClass("success").html("You can only check five checkboxes!");
                        $(this).attr("checked",false);
                        //alert("count = " + ClikCount);
                    }
                }
                else {
                    ClikCount = ClikCount - 1;
                    warning.addClass("success").removeClass("error").html("Check up to 5 checkboxes in total.");
                }
            });

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

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