简体   繁体   English

jQuery / js分离2个下拉值

[英]jQuery/js separating 2 drop down values

I'm using Eric Hynds jQuery MultiSelect Widget. 我正在使用Eric Hynds jQuery MultiSelect Widget。 I'm actually using 2 separate widgets that when checked, creates a dynamic checkbox w/ value attached to a corresponding 'Main' checkbox if 'Main' checkbox is checked. 我实际上使用的是2个单独的小部件,当选中它们时,如果选中了“主”复选框,则会创建一个带有附加到相应“主”复选框的值的动态复选框。 Please see my fiddle with comments inside to illustrate my problem: http://jsfiddle.net/3u7Xj/52/ 请查看我的提琴,里面带有注释以说明我的问题: http : //jsfiddle.net/3u7Xj/52/

How to separate the 2 widgets to show in corresponding sections and still only allow the user to choose 2 combined from either? 如何将2个小部件分开以显示在相应的部分中,而仍然只允许用户从其中一个中选择2个?

$(document).ready(function() {
    $(".multiselect").multiselect({
        header: "Choose up to 5 areas total",
        click: function (event, ui) {

            if (ui.checked && $(".multiselect").children(":checked").length >= 2) {
                return false;
            }

            var lbl = ui.value;
            if(ui.checked){
                var ctrl = '<input type="checkbox" name="chk" checked="checked" class="chk" id="'+lbl+'">';
                $("[id^=Main]:checked").each(function(){
                    $(this).nextAll('.holder:first').append('<div>'+ctrl+lbl+'</div>');    
                });
            }
            else {
                $("[id^=Main]:checked").each(function(){
                    $(this).nextAll('.holder:first').find('div input[id="'+lbl+'"]').parent().remove();
                });
            }

        },
        selectedList:5
    });
});

So I modified the condition and it works as it should, I hope that it will suit you. 因此,我修改了条件,使其可以正常工作,希望它适合您。

var number1=$("#dropdown1").children(":checked").length,
    number2=$("#dropdown2").children(":checked").length;

if (ui.checked && ((number1 + number2 >=4) || $(this).children(":checked").length >= 2)){
    return false;
}

Demo here 在这里演示

If you want to only two from both selects and modify the condition as follows: 如果您只想从两个中选择两个,则按如下所示选择和修改条件:

var number1=$("#dropdown1").children(":checked").length,
    number2=$("#dropdown2").children(":checked").length;

if (ui.checked && ((number1 + number2 >=2) || $(this).children(":checked").length >= 2)){
    return false;
}

Demo here 在这里演示

Adapted to the requirements in the comment 适应注释中的要求

individual segments inserted into the div. 插入div的单个段。 Adjusted control function click 调整控制功能请点击

to: 至:

$(this).parent("div").find("[id^=Main]:checked").each(function(){
      $(this).nextAll('.holder:first').append('<div>'+ctrl+lbl+'</div>');    
});

Demo here 在这里演示

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

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