搜索输入,其中value包含在字符串中,与字符串不匹配

时间:2016-07-02 17:14:16

标签: jquery

我有这段代码:

<input type="submit" class="hobsubmit" value="Done" name="submited">
var harlhar = ['Acting', 'Art', 'Archery', 'American football', 'Baseball', 'Basketball', 'Birdwatching', 'Biology', 'Chess', 'Chemistry', 'Cooking', 'Collecting art', 'Collecting books', 'Collecting cards', 'Collecting coins', 'Collecting comic books']
var chosen_ones = [];
var results = [];
$(".HInp").bind("keyup paste", function(e) {
    if (e.which >= 65 && e.which <= 90 || e.which == 8) {
        var value = $(this).val();
        $(".Hresult").empty();
        if (value.length != 0) {
            function filter(letter) {
                var len = har.length;
                var i = 0;
                while (i < len) {
                    if (har[i].indexOf(letter) == 0 || har[i].indexOf(letter.toUpperCase()) == 0) {
                        results.push(har[i]);
                        var clc = $(".ListOI").children().children("div:contains('" + harlhar[i] + "')").attr('click-c');
                        $(".Hresult").append("<li data-n=" + i + " class='Respan'>" + harlhar[i] + "</li>");
                        $(".Hresult").find("[data-n='" + i + "']").wrap('<div data-n=' + i + ' class="music" click-c="' + clc + '"></div>');
                        $(".music").find("[data-n='" + i + "']").prepend('<div class="addsigrn">+</div>');

                    }
                    i++;
                }
                return results.join("<li class='Respan'>");
            }

问题是,如果我输入Art,它只会给我Art,但我希望它也会返回Collecting art。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

您的兴趣点是:

if (har[i].indexOf(letter) == 0 || har[i].indexOf(letter.toUpperCase()) == 0) {

我建议你直接使用:

if (harlhar[i].toUpperCase().indexOf(letter.toUpperCase()) != -1) {

我试图找出你的HTML,所以不要过多关注它。

所以片段:

var harlhar = ['Acting', 'Art', 'Archery', 'American football', 'Baseball', 'Basketball', 'Birdwatching', 'Biology', 'Chess', 'Chemistry', 'Cooking', 'Collecting art', 'Collecting books', 'Collecting cards', 'Collecting coins', 'Collecting comic books']
var chosen_ones = [];
var results = [];

function filter(letter) {
  var len = harlhar.length;
  var i = 0;
  results = [];
  while (i < len) {
    if (harlhar[i].toUpperCase().indexOf(letter.toUpperCase()) != -1) {
      results.push(harlhar[i]);
      var clc = $(".ListOI").children().children("div:contains('" + harlhar[i] + "')").attr('click-c');
      $(".Hresult").append("<li data-n=" + i + " class='Respan'>" + harlhar[i] + "</li>");
      $(".Hresult").find("[data-n='" + i + "']").wrap('<div data-n=' + i + ' class="music" click-c="' + clc + '"></div>');
      $(".music").find("[data-n='" + i + "']").prepend('<div class="addsigrn">+</div>');

    }
    i++;
  }
  return results.join("<li class='Respan'>");
}
$(function () {
  $(".HInp").bind("keyup paste", function(e) {
    if (e.which >= 65 && e.which <= 90 || e.which == 8) {
      var value = $(this).val();
      $(".Hresult").empty();
      if (value.length != 0) {
        filter(value);
      }
    }
  });
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>

<input type="text" class="HInp">
<input type="submit" class="hobsubmit" value="Done" name="submited">

<ul class="Hresult"></ul>