2017-01-10 121 views
0

我想製作一個簡單的應用程序,調用api並獲取用戶數據。當用戶數據顯示在頁面上時,我想用他們的用戶名搜索用戶。嘗試了很多次,但沒有成功。任何人都可以幫忙嗎?簡單的搜索功能與jQuery搜索項不起作用

var base = "https://api.twitch.tv/kraken/channels/"; 
 
var key = "myKey&callback"; 
 
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", 
 
      "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas", 
 
      "TwinGalaxiesLive","MedryBW", "frontender007","Gronkh"]; 
 

 

 

 
$(document).ready(function(){ 
 
    requestData(users); 
 
    searchUsers($("a")); 
 

 
}); 
 

 
function requestData (arr) { 
 
    arr.forEach (function (user) { 
 
    var url = base + user + '/?client_id=' + key + '&callback=?'; 
 
    $.getJSON(url, function (json) { 
 
     
 
     displayChannel(json); 
 
     
 
    }); 
 
    }); 
 
} 
 

 
function displayChannel(obj) { 
 
    var name, status, logo, url, game; 
 
    
 
    name = obj.display_name; 
 
    status = obj.status; 
 
    logo = obj.logo; 
 
    url = obj.url; 
 
    game = obj.game; 
 
    
 
    var channel,onoff; 
 

 
    if (status && status.length > 16) { 
 
    \t status = status.substr(0, 16) + "...."; 
 
    } 
 

 

 
    if (game === null) { 
 
    \t onoff = "offline"; 
 
    } 
 
    else { 
 
    \t onoff = "online"; 
 
    } 
 
    
 
    channel = "<a>"; 
 
    channel += "<h3>" + name + "</h3>"; 
 
    channel += "<img src='" + logo + "'>"; 
 
    channel += "<p>" + status + "</p>"; 
 
    $(channel).attr('class', onoff); 
 

 
    $("#container").append(channel); 
 
    
 
    
 
} 
 

 

 
function searchUsers(arr) { 
 
\t var searchBox = $("input[type='text']"); 
 

 
\t $(searchBox).keyup(function(){ 
 

 
\t \t var chars = $(searchBox).val(); 
 

 
\t \t $("arr").each(function (item){ 
 
\t \t \t if($(item).find('h3').text().indexOf(chars) < 0) { 
 
\t \t \t \t $(this).parent().closest('a').hide(); 
 
\t \t \t } 
 
\t \t \t else { 
 
\t \t \t \t $(this).parent().closest('a').show(); 
 
\t \t \t } 
 
\t \t }); 
 
\t }); 
 
}
/* http://meyerweb.com/eric/tools/css/reset/ 
 
    v2.0 | 20110126 
 
    License: none (public domain) 
 
*/ 
 

 
html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
\t margin: 0; 
 
\t padding: 0; 
 
\t border: 0; 
 
\t font-size: 100%; 
 
\t font: inherit; 
 
\t vertical-align: baseline; 
 
} 
 
/* HTML5 display-role reset for older browsers */ 
 
article, aside, details, figcaption, figure, 
 
footer, header, hgroup, menu, nav, section { 
 
\t display: block; 
 
} 
 
body { 
 
\t line-height: 1; 
 
} 
 
ol, ul { 
 
\t list-style: none; 
 
} 
 
blockquote, q { 
 
\t quotes: none; 
 
} 
 
blockquote:before, blockquote:after, 
 
q:before, q:after { 
 
\t content: ''; 
 
\t content: none; 
 
} 
 
table { 
 
\t border-collapse: collapse; 
 
\t border-spacing: 0; 
 
} 
 

 
/************************************************************/ 
 

 
header { 
 
    width: 960px; 
 
    height: auto; 
 
    margin: 20px auto; 
 
    overflow: hidden; 
 
} 
 

 
header form { 
 
    float: right; 
 
} 
 

 
header form input { 
 
    font-size: 18px; 
 
    padding: 5px 10px; 
 
} 
 
#container { 
 
    width: 960px; 
 
    height: 500px; 
 
    background-color: #cecece; 
 
    margin: 50px auto; 
 
} 
 

 
a { 
 
    width: 140px; 
 
    height: 80px; 
 
    margin: 40px; 
 
    float: left; 
 

 
} 
 

 
a img { 
 
    width: 140px; 
 
    height: 80px; 
 
} 
 

 
h3 { 
 
    margin-bottom: 10px; 
 
    float: left; 
 
} 
 

 
p { 
 
    font-style: italic; 
 
    font-size: 13px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <link rel="stylesheet" href="twitch.css"> 
 
    <title> Twitch TV</title> 
 
</head> 
 
<body> 
 
    
 
    <header> 
 
    \t <form action="#"> 
 
    \t \t <input type="text"> 
 
    \t \t <input type="radio"> All 
 
    \t \t <input type="radio"> Online 
 
    \t \t <input type="radio"> Offline 
 
    \t </form> 
 
    </header> 
 

 
    <div id="container"> 
 

 
    
 
    
 
    </div> 
 
    
 

 
<script src="https://code.jquery.com/jquery-3.1.0.js"></script> 
 
<script src="twitch.js"></script> 
 
</body> 
 
</html>

回答

1

我試圖更新您的代碼,我希望這有助於

var base = "https://api.twitch.tv/kraken/users/"; 
var key = "myKey&callback"; 
var users = ["ESL_SC2", "OgamingSC2", "cretetion", "freecodecamp", 
      "storbeck", "habathcx", "RobotCaleb", "noobs2ninjas", 
      "TwinGalaxiesLive","MedryBW", "frontender007","Gronkh"]; 



$(document).ready(function(){ 
    requestData(users); 
    searchUsers(users); 

}); 

function requestData (arr) { 
    arr.forEach (function (user) { 
    var url = base + user + '/?client_id=' + key + '&callback=?'; 
    $.getJSON(url, function (json) { 

     displayChannel(json); 

    }); 
    }); 
} 

function displayChannel(obj) { 
    var name, status, logo, url, game; 

    name = obj.display_name; 
    status = obj.status; 
    logo = obj.logo; 
    url = obj.url; 
    game = obj.game; 

    var channel,onoff; 

    if (status && status.length > 16) { 
    status = status.substr(0, 16) + "...."; 
    } 


    if (game === null) { 
    onoff = "offline"; 
    } 
    else { 
    onoff = "online"; 
    } 

    channel = "<a id="+name+">"; 
    channel += "<h3>" + name + "</h3>"; 
    channel += "<img src='" + logo + "'>"; 
    channel += "<p>" + status + "</p>"; 
    $(channel).attr('class', onoff); 

    $("#container").append(channel); 


} 


function searchUsers(arr) { 
    var searchBox = $("input[type='text']"); 

     var chars = $(searchBox).val(); 

     $.each(arr, function(i, val) { 

      if(val==chars) { 
       $('a#'+chars).siblings().hide(); 
      } 
      else { 
       $(this).parent().closest('a').show(); 
      } 
     }); 

} 
$("input[type='text']").change(function(){ 

    searchUsers(users); 
});