问题
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