2014-01-08 59 views
3

我在页面中有2个多重选择,并且我需要在保留搜索能力的同时将某些选项先移到第二位。修改后无法搜索多选择

的问题是,当我使用搜索输入,它的选择还原到其原来的选项...

这里是jQuery的搜索功能:

jQuery.fn.filterByText = function(textbox) { 
    return this.each(function() { 
     var select = this; 
     var options = []; 
     $(select).find('option').each(function() { 
      options.push({value: $(this).val(), text: $(this).text()}); 
     }); 
     $(select).data('options', options); 

     $(textbox).bind('change keyup', function() { 
      var options = $(select).empty().data('options'); 
      var search = $.trim($(this).val()); 
      var regex = new RegExp(search,"gi"); 

      $.each(options, function(i) { 
       var option = options[i]; 
       if(option.text.match(regex) !== null) { 
        $(select).append(
         $('<option>').text(option.text).val(option.value) 
        ); 
       } 
      }); 
     }); 
    }); 
}; 

这里是JS提琴:http://jsfiddle.net/C2XXR/

* 我认为,问题出在选择的变量,但对如何解决它*

由于不知道!

回答

1

我已经更新了小提琴。 http://jsfiddle.net/C2XXR/2/

我已经更新了左右转移的代码。你必须改变选项数组本身得到的过滤器,在dom中添加它们将不起作用。在更改后的代码中,一个问题是我们从右向左或从左向右添加它将被添加到目标选择的最后位置。

请检查并让我知道这是你想要的。

下面是主要改变的功能。稍后,您可以创建一个我认为常用的功能。代码可以进一步优化。

$('[id^=\"btnRight\"]').click(function (e) { 

     var selected = $(this).parent().prev('select'); 
     var target = $(this).parent().next('select'); 

     target.find('option[value="999"]').remove() 

     var options = selected.data('options');  
     var selectedVal = selected.find('option:selected').val()  

     var tempOption = []; 
     $.each(options, function(i) { 
       var option = options[i]; 
       if(option.value != selectedVal) { 
        tempOption.push(option); 
       } 
      }); 

     var targetOptions = target.data('options'); 
     targetOptions.push({value: selected.find('option:selected').val(), text: selected.find('option:selected').text()});   
     target.data('options', targetOptions); 

     selected.find('option:selected').remove().appendTo('#isselect_code'); 
     selected.data('options', tempOption); 
    }); 

    $('[id^=\"btnLeft\"]').click(function (e) { 

     var selected = $(this).parent().next('select'); 
     var target = $(this).parent().prev('select'); 

     var options = selected.data('options');  
     var selectedVal = selected.find('option:selected').val()  

     var tempOption = []; 
     $.each(options, function(i) { 
       var option = options[i]; 
       if(option.value != selectedVal) { 
        tempOption.push(option); 
       } 
      }); 
     if(tempOption.length == 0) 
     { 
      // add 999 here 
     } 

     var targetOptions = target.data('options'); 
     targetOptions.push({value: selected.find('option:selected').val(), text: selected.find('option:selected').text()});   

     target.data('options', targetOptions); 


     selected.find('option:selected').remove().appendTo('#canselect_code');; 
     selected.data('options', tempOption); 
    }); 
+0

虽然有一个问题,用右边的选择框。所有选项叠加,而不是一次显示一个 –

+0

欢迎。请不要忘记赏金。我在Stackoverflow的第一个赏金。谢谢 !! –

+0

没问题,我无法立即给予赏金,但在24小时内,stackoverflow rules..Will你请帮助我与正确的选择框呢?左边的一个很棒!但是,正确的一个并没有:(谢谢! –

0

与您的代码的问题是,你点击btnRightbtnLeft你的每个select的选项集合不更新,经过这么点击每个按钮后,尝试打电话给你filterByText,如下所示:

$('[id^=\"btnRight\"]').click(function (e) { 
     $(this).parent().next('select').find('option[value="999"]').remove() 
     $(this).parent().prev('select').find('option:selected').remove().appendTo('#isselect_code'); 
     $('#canselect_code').filterByText($('#textbox'), true); 
     $('#isselect_code').filterByText($('#textbox1'), true) 

    }); 

    $('[id^=\"btnLeft\"]').click(function (e) { 

     $(this).parent().next('select').find('option:selected').remove().appendTo('#canselect_code'); 
     $('#canselect_code').filterByText($('#textbox'), true); 
     $('#isselect_code').filterByText($('#textbox1'), true) 
}); 
+0

这不会工作,因为选项将更新为一个空的数组 –