Matching exactly option and optgroup

和自甴很熟 提交于 2020-06-29 07:07:15

问题


I got an issue with following script when the select-options become long:

Solution with few options http://jsfiddle.net/hsQjh/5/

Long options with issue: http://jsfiddle.net/hsQjh/6/

My first select-box option is up to 40+ of options, thus the second select-box would be up to 40+ of optgroup as well, when I picked option '2' on first box, second box are populated all optgroup that is numbered started with 2, which is optgroup '2', '20' - '29' are populated, the same are happen to other options which is had selected too.

Could it be match the 'IDs' exactly in between option's value and optgroup's label?

function filterActivity(e){
     var ids = $('#filterActivity + div input:checked').map(function(i) {
        return $(this).val().replace(/ .*/, '');
     }).get(); // Retrieve checked IDs

     $('#filterSubActivity + div div label').each(function() { // Show matching options
        $(this).toggle($.inArray($('input', this).val()[0], ids) > -1);
     });

     $('#filterSubActivity + div label.optGroup').each(function() { // Show matching groups
        $(this).toggle($(this).next().find('label:visible').length > 0);
     });
}

Thanks.


回答1:


Use a regex based filter

$('#filterSubActivity + div div label').each(function() { // Show matching options
    $(this).toggle($.inArray($('input', this).val().match(/^\d+/)[0], ids) > -1);
});

Demo: Fiddle



来源:https://stackoverflow.com/questions/15628046/matching-exactly-option-and-optgroup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!