2017-07-14 26 views
0

我使用typeahead作爲一個非常簡單的web應用程序的自動建議。我對JavaScript和jQuery非常陌生。網絡應用程序是一個輸入,用於搜索iTunes商店並在用戶輸入時提示結果。更新帶鍵入值的隱藏輸入?

我有一個版本的工作,它將根據用戶輸入的內容檢索歌曲名稱。然而,我希望能夠做的是使用其他值更新一些隱藏字段(例如,藝術家姓名,曲目ID,預覽網址等)。我正在嘗試使用filter選項來完成此操作,但現在我只搜索undefined

此外,一般提示重新:JavaScript和jQuery將不勝感激。這是我想要工作的下一個技能。

對於相關代碼:

的index.html

<!DOCTYPE html> 
<html lang="en"> 

<head> 
    <meta http-equiv="Content-Language" content="en-us"> 
    <title>HOLLAND</title> 
    <meta charset="utf-8"> 
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"> 
    <script src="//code.jquery.com/jquery-2.1.4.min.js"></script> 
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> 
    <script src="//netsh.pp.ua/upwork-demo/1/js/typeahead.js"></script> 
    <style> 
     h1 { 
      font-size: 20px; 
      color: #111; 
     } 
     .content { 
      width: 80%; 
      margin: 0 auto; 
      margin-top: 50px; 
     } 

     input[type=submit] { 
      border: none; 
      border-radius: 8px 8px 8px 8px; 
      height: 47px; 
      width: 60px; 
      vertical-align: bottom; 
     } 

     .tt-hint, 
     .song { 
      border: 2px solid #CCCCCC; 
      border-radius: 8px 8px 8px 8px; 
      font-size: 24px; 
      height: 45px; 
      line-height: 30px; 
      outline: medium none; 
      padding: 8px 12px; 
      width: 400px; 
     } 

     .tt-dropdown-menu { 
      width: 400px; 
      margin-top: 5px; 
      padding: 8px 12px; 
      background-color: #fff; 
      border: 1px solid #ccc; 
      border: 1px solid rgba(0, 0, 0, 0.2); 
      border-radius: 8px 8px 8px 8px; 
      font-size: 18px; 
      color: #111; 
      background-color: #F1F1F1; 
     } 

    </style> 
    <script> 

    var resultList, d; 

    $(document).ready(function() { 
      $('input.song').typeahead({ 
       name: 'song', 
       remote: { 
        url: '/Playlist/search.php?query=%QUERY', 
        filter: function(data) { <-- THIS IS WHERE I NEED ASSISTANCE 

           retval = []; 
           for (var i = 0; i < data.length; i++) { 
            retval.push({ 
             value: data[i].trackId, 
             artistName: data[i].artistName, 
            }); 
           } 

           return retval; 
          } 
         } 

      }).on('song:select', function(ev, suggestion) { 
       $('#songID').val(d[resultList.indexOf(suggestion)].trackId); 
      }); 

     }); 

     function printArray(arr){ 
      for(var i = 0; i < arr.length; i++){ 
       if(arr[i] instanceof Array){ 
        printArray(arr[i]); 
       }else{ 
        console.log(arr[i]); 
       } 
      } 
     } 

    </script> 
</head> 

<body> 
    <div class="content"> 
     <h2>Playlist</h2> 
     <form> 
      <h1>Search Apple Music</h1> 
      <input type="text" name="song" size="30" class="song" placeholder="Please enter song name"> 
      <input type="hidden" name ="songID"> 
      <input type="submit" name="submit" value="Add"> 
     </form> 
    </div> 
</body> 

</html> 

的search.php從iTunes

<?php 
    $key = $_GET['query']; 

    $searchString = "https://itunes.apple.com/search?term=" . urlencode($key) . "&limit=10&entity=song"; 
    $array = array(); 

    // Initiate curl 
    $ch = curl_init(); 
    //No header return 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    //Use proxy 
    curl_setopt($ch, CURLOPT_PROXY, "naproxy.gm.com"); 
    curl_setopt($ch, CURLOPT_PROXYPORT, "80"); 
    // Disable SSL verification 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    // Will return the response, if false it print the response 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    // Set the url 
    curl_setopt($ch, CURLOPT_URL, $searchString); 
    // Execute 
    $result=curl_exec($ch); 

    // Will dump a beauty json :3 
    if(curl_errno($ch)) { 
     echo curl_error($ch); 
    } 
    else{ 
     $obj = json_decode($result, true); 
    } 

    // Closing 
    curl_close($ch); 

    /* 
    foreach($obj['results'] as $row => $value) { 
     $array[] = $value['trackName'] . " by " . $value['artistName']; 
    } 
    */ 

    foreach($obj['results'] as $row => $value) { 
     $array[$row] = array 
      (
       'trackId', $value['trackId'], 
       'trackName', $value['trackName'], 
       'artistName',$value['artistName'], 
       'previewURL', $value ['artworkUrl30'] 
      ); 
    } 

    echo $array; 

?> 

響應

{ 
    "resultCount": 10, 
    "results": [ 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 902180909, 
     "trackId": 902180917, 
     "artistName": "Lady Gaga", 
     "collectionName": "Born This Way (Bonus Track Version)", 
     "trackName": "Born This Way", 
     "collectionCensoredName": "Born This Way (Bonus Track Version)", 
     "trackCensoredName": "Born This Way", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902180909?i=902180917&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902180909?i=902180917&uo=4", 
     "previewUrl": "http:\/\/a463.phobos.apple.com\/us\/r30\/Music1\/v4\/d5\/3c\/07\/d53c0773-ff35-1fe5-9be2-815038285c08\/mzaf_4465142801402798156.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg", 
     "collectionPrice": 10.99, 
     "trackPrice": 1.29, 
     "releaseDate": "2013-01-01T08:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 22, 
     "trackNumber": 2, 
     "trackTimeMillis": 260258, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Pop", 
     "isStreamable": true 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 902096524, 
     "trackId": 902096558, 
     "artistName": "Lady Gaga", 
     "collectionName": "Born This Way", 
     "trackName": "Born This Way", 
     "collectionCensoredName": "Born This Way", 
     "trackCensoredName": "Born This Way", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902096524?i=902096558&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way\/id902096524?i=902096558&uo=4", 
     "previewUrl": "http:\/\/a459.phobos.apple.com\/us\/r30\/Music1\/v4\/c7\/2a\/83\/c72a8341-88b7-c65d-09dc-c333de92bb0c\/mzaf_4381737056492150878.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is1.mzstatic.com\/image\/thumb\/Music3\/v4\/b9\/d2\/e8\/b9d2e809-eb0b-d2df-193b-5bc99b5e5ecf\/source\/100x100bb.jpg", 
     "collectionPrice": 7.99, 
     "trackPrice": 1.29, 
     "releaseDate": "2011-02-11T08:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 14, 
     "trackNumber": 2, 
     "trackTimeMillis": 260258, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Pop", 
     "isStreamable": true 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 902180909, 
     "trackId": 902180935, 
     "artistName": "Lady Gaga", 
     "collectionName": "Born This Way (Bonus Track Version)", 
     "trackName": "The Edge of Glory", 
     "collectionCensoredName": "Born This Way (Bonus Track Version)", 
     "trackCensoredName": "The Edge of Glory", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/the-edge-of-glory\/id902180909?i=902180935&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/the-edge-of-glory\/id902180909?i=902180935&uo=4", 
     "previewUrl": "http:\/\/a642.phobos.apple.com\/us\/r30\/Music4\/v4\/bb\/93\/f6\/bb93f6dd-b09f-6378-9097-e41fab0a50e7\/mzaf_2685820553066586847.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg", 
     "collectionPrice": 10.99, 
     "trackPrice": 1.29, 
     "releaseDate": "2013-01-01T08:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 22, 
     "trackNumber": 17, 
     "trackTimeMillis": 321545, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Pop", 
     "isStreamable": true 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 902180909, 
     "trackId": 902180934, 
     "artistName": "Lady Gaga", 
     "collectionName": "Born This Way (Bonus Track Version)", 
     "trackName": "Yo\u00fc and I", 
     "collectionCensoredName": "Born This Way (Bonus Track Version)", 
     "trackCensoredName": "Yo\u00fc and I", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/yo%C3%BC-and-i\/id902180909?i=902180934&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/yo%C3%BC-and-i\/id902180909?i=902180934&uo=4", 
     "previewUrl": "http:\/\/a37.phobos.apple.com\/us\/r30\/Music1\/v4\/b3\/d0\/51\/b3d05174-db69-be68-272b-1b1dd35360d9\/mzaf_7395703188902498297.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg", 
     "collectionPrice": 10.99, 
     "trackPrice": 1.29, 
     "releaseDate": "2011-05-23T07:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 22, 
     "trackNumber": 16, 
     "trackTimeMillis": 307364, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Pop", 
     "isStreamable": true 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 716415763, 
     "trackId": 716415953, 
     "artistName": "Lady Gaga", 
     "collectionName": "NOW That's What I Call a Workout", 
     "trackName": "Born This Way (Bimbo Jones Club Remix)", 
     "collectionCensoredName": "NOW That's What I Call a Workout", 
     "trackCensoredName": "Born This Way (Bimbo Jones Club Remix)", 
     "collectionArtistId": 4035426, 
     "collectionArtistName": "Various Artists", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way-bimbo-jones-club-remix\/id716415763?i=716415953&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way-bimbo-jones-club-remix\/id716415763?i=716415953&uo=4", 
     "previewUrl": "http:\/\/a294.phobos.apple.com\/us\/r30\/Music3\/v4\/25\/47\/e8\/2547e80d-c74c-7cf4-7905-041c7f7ec259\/mzaf_1883593890824568199.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is5.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is5.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is5.mzstatic.com\/image\/thumb\/Music6\/v4\/a5\/ed\/99\/a5ed99e5-ca11-2a34-a72d-e86b2e11a1ba\/source\/100x100bb.jpg", 
     "collectionPrice": 11.99, 
     "trackPrice": -1, 
     "releaseDate": "2012-12-18T08:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 18, 
     "trackNumber": 1, 
     "trackTimeMillis": 284453, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Fitness & Workout", 
     "isStreamable": false 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 902180909, 
     "trackId": 902180936, 
     "artistName": "Lady Gaga", 
     "collectionName": "Born This Way (Bonus Track Version)", 
     "trackName": "Born This Way", 
     "collectionCensoredName": "Born This Way (Bonus Track Version)", 
     "trackCensoredName": "Born This Way (The Country Road Version)", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way-the-country-road-version\/id902180909?i=902180936&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/born-this-way-the-country-road-version\/id902180909?i=902180936&uo=4", 
     "previewUrl": "http:\/\/a470.phobos.apple.com\/us\/r30\/Music4\/v4\/76\/79\/3d\/76793d4c-0683-3793-0b02-7e8ced3ec1e1\/mzaf_5908951428023709023.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg", 
     "collectionPrice": 10.99, 
     "trackPrice": 1.29, 
     "releaseDate": "2011-05-23T07:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 22, 
     "trackNumber": 18, 
     "trackTimeMillis": 261276, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Pop", 
     "isStreamable": true 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 902180909, 
     "trackId": 902180916, 
     "artistName": "Lady Gaga", 
     "collectionName": "Born This Way (Bonus Track Version)", 
     "trackName": "Marry the Night", 
     "collectionCensoredName": "Born This Way (Bonus Track Version)", 
     "trackCensoredName": "Marry the Night", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/marry-the-night\/id902180909?i=902180916&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/marry-the-night\/id902180909?i=902180916&uo=4", 
     "previewUrl": "http:\/\/a211.phobos.apple.com\/us\/r30\/Music4\/v4\/74\/30\/24\/743024be-2c46-b876-5c39-ba6bdf94ec72\/mzaf_1169912516356657975.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg", 
     "collectionPrice": 10.99, 
     "trackPrice": 1.29, 
     "releaseDate": "2013-01-01T08:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 22, 
     "trackNumber": 1, 
     "trackTimeMillis": 264523, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Pop", 
     "isStreamable": true 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 902180909, 
     "trackId": 902180938, 
     "artistName": "Lady Gaga", 
     "collectionName": "Born This Way (Bonus Track Version)", 
     "trackName": "Judas", 
     "collectionCensoredName": "Born This Way (Bonus Track Version)", 
     "trackCensoredName": "Judas (DJ White Shadow Remix)", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/judas-dj-white-shadow-remix\/id902180909?i=902180938&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/judas-dj-white-shadow-remix\/id902180909?i=902180938&uo=4", 
     "previewUrl": "http:\/\/a1915.phobos.apple.com\/us\/r30\/Music4\/v4\/20\/95\/05\/2095055f-c724-b689-72bc-d0d747d5379d\/mzaf_3035200198497304749.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg", 
     "collectionPrice": 10.99, 
     "trackPrice": 1.29, 
     "releaseDate": "2011-05-23T07:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 22, 
     "trackNumber": 19, 
     "trackTimeMillis": 247483, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Pop", 
     "isStreamable": true 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 277293880, 
     "collectionId": 902180909, 
     "trackId": 902180920, 
     "artistName": "Lady Gaga", 
     "collectionName": "Born This Way (Bonus Track Version)", 
     "trackName": "Judas", 
     "collectionCensoredName": "Born This Way (Bonus Track Version)", 
     "trackCensoredName": "Judas", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/lady-gaga\/id277293880?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/judas\/id902180909?i=902180920&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/judas\/id902180909?i=902180920&uo=4", 
     "previewUrl": "http:\/\/a53.phobos.apple.com\/us\/r30\/Music4\/v4\/bc\/9b\/d5\/bc9bd511-00b9-ebad-73df-62cdf01a26b4\/mzaf_37.plus.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is3.mzstatic.com\/image\/thumb\/Music1\/v4\/94\/8f\/b9\/948fb9ce-de93-fe33-e7b5-a8bdf5580178\/source\/100x100bb.jpg", 
     "collectionPrice": 10.99, 
     "trackPrice": 1.29, 
     "releaseDate": "2011-04-15T07:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 22, 
     "trackNumber": 4, 
     "trackTimeMillis": 249071, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Pop", 
     "isStreamable": true 
    }, 
    { 
     "wrapperType": "track", 
     "kind": "song", 
     "artistId": 412537193, 
     "collectionId": 446108937, 
     "trackId": 446108958, 
     "artistName": "Born This Way", 
     "collectionName": "Poker Face - Single", 
     "trackName": "Poker Face", 
     "collectionCensoredName": "Poker Face - Single", 
     "trackCensoredName": "Poker Face", 
     "artistViewUrl": "https:\/\/itunes.apple.com\/us\/artist\/born-this-way\/id412537193?uo=4", 
     "collectionViewUrl": "https:\/\/itunes.apple.com\/us\/album\/poker-face\/id446108937?i=446108958&uo=4", 
     "trackViewUrl": "https:\/\/itunes.apple.com\/us\/album\/poker-face\/id446108937?i=446108958&uo=4", 
     "previewUrl": "http:\/\/a578.phobos.apple.com\/us\/r30\/Music\/1b\/4b\/13\/mzm.uzkuoblk.aac.p.m4a", 
     "artworkUrl30": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/95\/b5\/95\/95b59505-1d03-6a6d-4e36-e3b7219f7605\/source\/30x30bb.jpg", 
     "artworkUrl60": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/95\/b5\/95\/95b59505-1d03-6a6d-4e36-e3b7219f7605\/source\/60x60bb.jpg", 
     "artworkUrl100": "http:\/\/is4.mzstatic.com\/image\/thumb\/Music6\/v4\/95\/b5\/95\/95b59505-1d03-6a6d-4e36-e3b7219f7605\/source\/100x100bb.jpg", 
     "collectionPrice": 1.98, 
     "trackPrice": 0.99, 
     "releaseDate": "2011-06-17T07:00:00Z", 
     "collectionExplicitness": "notExplicit", 
     "trackExplicitness": "notExplicit", 
     "discCount": 1, 
     "discNumber": 1, 
     "trackCount": 2, 
     "trackNumber": 1, 
     "trackTimeMillis": 238653, 
     "country": "USA", 
     "currency": "USD", 
     "primaryGenreName": "Dance", 
     "isStreamable": true 
    } 
    ] 
} 

我懷疑的問題是如何我正在返回/解析陣列。當我查看控制檯時,resultList被設置,但它看起來像我想要的鍵(例如trackId,trackName,artistName等)實際上被存儲爲數組中的值。

非常感謝您的協助。我正在嘗試使用數組中返回的trackID的值更新隱藏的輸入歌曲ID(我還將添加其他一些內容,例如藝術家和預覽URL)。

謝謝!

+0

添加ID = 「songID」 你的輸入。 jQuery中的'#'指的是元素的ID,就像'.'指的是類。 – PiLHA

+0

感謝您的指針,這絕對是它的一部分。我認爲我對返回的json有麻煩。我已經用我的回覆數據看起來更新了我的答案。 – Soulfire

+0

add'eval(data);'filter':function(data)''並檢查json是否被返回。如果這是好的,下一步是做json處理來檢索相關信息。 – PiLHA

回答

0

一點改變你這樣的代碼:

filter: function(data) { <-- THIS IS WHERE I NEED ASSISTANCE 
    $('name["songID"]').val(data[i].trackId); 
retval = []; 
.............. 
......... 
... 
+0

我不認爲這是正確的。我懷疑我需要使用'select'自定義事件。它如何知道哪個trackId放入隱藏域? (我返回一個結果列表最多10個項目)。 – Soulfire