2014-01-08 62 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

這不會工作,因爲選項將更新爲一個空的數組 –