【问题标题】:Matching exactly option and optgroup完全匹配 option 和 optgroup
【发布时间】:2013-03-26 02:00:22
【问题描述】:

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

选项少的解决方案http://jsfiddle.net/hsQjh/5/

有问题的长选项:http://jsfiddle.net/hsQjh/6/

我的第一个选择框选项最多有 40 多个选项,因此当我第一次选择选项“2”时,第二个选择框也最多有 40 多个 optgroup框,第二个框填充所有以 2 开头的 optgroup,即 optgroup '2', '20' - '29'已填充,其他已选择的选项也会发生同样的情况。

是否可以完全匹配选项值和选项组标签之间的“ID”?

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);
     });
}

谢谢。

【问题讨论】:

    标签: jquery drop-down-menu optgroup


    【解决方案1】:

    使用基于正则表达式的过滤器

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

    演示:Fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多