2

Web 上のリスト ボックスを検索する方法の例を見つけました。とても良さそうに聞こえますが、IE では機能しません。IE で動作させるためにコードを変更する方法はありますか?

テキスト ボックスに入力を開始すると、リスト ボックスのすべての値が消えます。

jQuery.fn.filterByText = function(textbox, selectSingleMatch) {
  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 = $(this).val().trim();
      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)
          );
        }
      });
      if (selectSingleMatch === true && $(select).children().length === 1) {
        $(select).children().get(0).selected = true;
      }
    });            
  });
};

$(function() {
  $('#select').filterByText($('#textbox'), true);
});
4

1 に答える 1

3

この行を変更します。

var search = $(this).val().trim();

これに:

var search = $.trim($(this).val());
于 2012-10-24T16:11:10.847 に答える