简体   繁体   English

我如何从JavaScript对象获得多个响应

[英]How do I get multiple responses form javascript object

I have the code below to search for songs listed in a javascript object. 我有以下代码来搜索javascript对象中列出的歌曲。 It only returns one result when I would like it to return every matching result. 当我希望它返回每个匹配的结果时,它只会返回一个结果。 Also it starts searching as soon as I start typing, but I would like it to search after three or more letters are typed. 同样,它会在我开始键入后立即开始搜索,但是我希望在键入三个或更多字母后进行搜索。

This is the html: 这是html:

<form>
    <h3>Music Search</h3>
        <input id="songname" type="text" autocomplete="off">       
        </form>

<div id="containera">
<div id="fullpath"></div>
</div>

This is the js: 这是js:

$(function () {
    var data = {
        "music": [{
        "Title": "",
        "Fullpath": ""
        },
        {
        "Title": "TNT",
        "Fullpath": "Audio\Albums\AC DC\TNT\Tnt.mp3"
  },
  {
  "Title": "03 - Wasted Sleepless Nights_Dark Room.mp3",
  "Fullpath": "Audio/Albums/Angels/Dark Room/03 - Wasted Sleepless Nights_Dark Room.mp3"
  },
  {
  "Title": "03 - Wasted Sleepless Nights.mp3",
  "Fullpath": "Audio/Albums/Angels/Dark Room/03 - Wasted Sleepless Nights_Dark Room.mp3"
  }, {
  "Title": "Iron Maiden - Wasted Years.mp3",
  "Fullpath": "Audio/Singles/Iron Maiden/Wasted Years.mp3"
  }]   
    };

var getProperties = function (Title) {
        return data.music.filter(function (elem) {
            var expTitle = Title.indexOf(elem.Title) >= 0;
            var expTitle = elem.Title.toUpperCase().indexOf

(Title.toUpperCase()) >= 0;
            return expTitle
        });
    }

    $("#songname").on("input paste",function() {
        var Title = $("#songname").val();
       var properties = getProperties(Title);

if (properties.length == 0) 
 {
$( "#title" ).empty();
$( "#fullpath" ).empty();
}
else {

    $("#title").text(properties[0].Title);
        $("#fullpath").html("<a href=\"file:///"+properties[0].Fullpath+"\">"+properties[0].Title+"<\/a>");

}
    });
});

I'm pretty new to jquery and can do some simple stuff, but I can't work this out. 我对jquery还是很陌生,可以做一些简单的事情,但是我无法解决。 This is local on a shared computer. 这在共享计算机上是本地的。 I have a jsfiddle if it helps. 我有一个jsfiddle,如果有帮助。 I would be greatful for any help. 我将不胜感激。

Add the condition to verify the input value length to proceed only if > 3. 添加条件以验证输入值的长度仅在> 3时才继续进行。

 $("#songname").on("input paste", function() {
    var Title = $("#songname").val();
    if (Title.length < 3) {
      return false;
    }

Secondly properties is already having multiple value, you just need to change looping. 其次, properties已经具有多个值,您只需要更改循环即可。

https://jsfiddle.net/k7rwa4uo/ https://jsfiddle.net/k7rwa4​​uo/

$(function() {
  var data = {
    "music": [{
      "Title": "",
      "Fullpath": ""
    }, {
      "Title": "TNT",
      "Fullpath": "Audio\Albums\AC DC\TNT\Tnt.mp3"
    }, {
      "Title": "03 - Wasted Sleepless Nights_Dark Room.mp3",
      "Fullpath": "Audio/Albums/Angels/Dark Room/03 - Wasted Sleepless Nights_Dark Room.mp3"
    }, {
      "Title": "03 - Wasted Sleepless Nights.mp3",
      "Fullpath": "Audio/Albums/Angels/Dark Room/03 - Wasted Sleepless Nights_Dark Room.mp3"
    }, {
      "Title": "Iron Maiden - Wasted Years.mp3",
      "Fullpath": "Audio/Singles/Iron Maiden/Wasted Years.mp3"
    }]
  };


  var getProperties = function(Title) {
    return data.music.filter(function(elem) {
      var expTitle = Title.indexOf(elem.Title) >= 0;
      var expTitle = elem.Title.toUpperCase().indexOf(Title.toUpperCase()) >= 0;
      return expTitle;
    });
  }

  $("#songname").on("input paste", function() {
    var Title = $("#songname").val();
    if (Title.length < 3) {
      return false;
    }
    var properties = getProperties(Title);

    $("#fullpath").html("");

    if (properties.length == 0) {
      $("#folder").empty();
      $("#subfolder").empty();
      $("#artist").empty();
      $("#album").empty();
      $("#number").empty();
      $("#title").empty();
      $("#fullpath").empty();
    } else {

      console.log(properties);

      for (var i = 0; i < properties.length; i++) {
        $("#cover").html("<img src=\" " + properties[i].Poster + " \" alt=\"\" width=\"100\">");
        $("#folder").text(properties[i].Folder);
        $("#subfolder").text(properties[i].SubFolder);
        $("#artist").text(properties[i].Artist);
        $("#album").text(properties[i].Album);
        $("#number").text(properties[i].Number);
        $("#title").text(properties[i].Title);

        $("#fullpath").append("<a href=\"file:///" + properties[i].Fullpath + "\">" + properties[i].Title + "<\/a><br/>");

      }

      // $("#fullpath1").html("<a href=\"file:///c:/"+properties[0].Folder +"\/"+properties[0].SubFolder+"\/"+properties[0].Artist+"\/"+properties[0].Album+"\/"+properties[0].Number+" - "+properties[0].Title+"."+properties[0].Type+"\">"+properties[0].Title+"<\/a>");

    }
  });
});

You need to use append instead of html when you are adding the song titles to your html. 将歌曲标题添加到html时,需要使用append而不是html。 You also need to iterate the result to display all the song titles like the code below. 您还需要迭代结果以显示所有歌曲标题,如下面的代码。

I also added a UL-list to your HTML to display the titles in a list. 我还向您的HTML添加了UL列表,以在列表中显示标题。

 $(function() { var data = { "music": [{ "Title": "", "Fullpath": "" }, { "Title": "TNT", "Fullpath": "Audio\\Albums\\AC DC\\TNT\\Tnt.mp3" }, { "Title": "03 - Wasted Sleepless Nights_Dark Room.mp3", "Fullpath": "Audio/Albums/Angels/Dark Room/03 - Wasted Sleepless Nights_Dark Room.mp3" }, { "Title": "03 - Wasted Sleepless Nights.mp3", "Fullpath": "Audio/Albums/Angels/Dark Room/03 - Wasted Sleepless Nights_Dark Room.mp3" }, { "Title": "Iron Maiden - Wasted Years.mp3", "Fullpath": "Audio/Singles/Iron Maiden/Wasted Years.mp3" }] }; var getProperties = function(Title) { return data.music.filter(function(elem) { var expTitle = Title.indexOf(elem.Title) >= 0; expTitle = elem.Title.toUpperCase().indexOf(Title.toUpperCase()) >= 0; return expTitle; }); } $("#songname").on("input paste", function() { var Title = $("#songname").val(); if (Title.length < 3) { return false; } var properties = getProperties(Title); if (properties.length == 0) { $("#folder").empty(); $("#subfolder").empty(); $("#artist").empty(); $("#album").empty(); $("#number").empty(); $("#title").empty(); $("#fullpath").empty(); } else { console.log(properties); //HERE IS THE ITERATION for (var i = 0; i < properties.length; i++) { $("#cover").html("<img src=\\" " + properties[i].Poster + " \\" alt=\\"\\" width=\\"100\\">"); $("#folder").text(properties[i].Folder); $("#subfolder").text(properties[i].SubFolder); $("#artist").text(properties[i].Artist); $("#album").text(properties[i].Album); $("#number").text(properties[i].Number); $("#title").text(properties[i].Title); $("#fullpath ul").append("<li><a href=\\"file:///" + properties[i].Fullpath + "\\">" + properties[i].Title + "<\\/a></li>"); } } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <h3>Music Search</h3> <input id="songname" type="text" autocomplete="off"> </form> <div id="containera"> <div id="fullpath"> <ul> </ul> </div> </div> <p> <p> Typing "wasted" should return all matches, three matches in this test. At the moment it only returns one. Also need to only search after three characters or more are entered. </p> 

The problem why it is returning one first result is because in properties, you are assigning the [0]th index value only, you have loop it and assign it. 之所以返回一个第一个结果的问题是因为在属性中,您仅分配了第[0]个索引值,进行了循环并分配了它。 Also, for filtering is nor proper in get 另外,由于过滤不正确

 return data.music.filter(function (elem) {
var MatchingTitle = []; // creating a array to holding all matching values
if(elem.Title.indexOf(Title)>=0){
MatchingTitle.push(elem.Title);
}
 //           var expTitle = Title.indexOf(elem.Title) >= 0;
   //         var expTitle = elem.Title.toUpperCase().indexOf

//(Title.toUpperCase()) >= 0;
            return MatchingTitle.join(''); // use this
        });


alert(properties.length);
    $("#cover").html("<img src=\" " + properties[0].Poster + " \" alt=\"\" width=\"100\">");
    $("#folder").text(properties[0].Folder);
    $("#subfolder").text(properties[0].SubFolder);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM