我有读取一个JSON文件,有很多在这信息的Node.js服务器。其中之一是一个人的ID,我在我的HTML页面中有一个搜索框我想为客户端找一个jquery方法(需要ajax)来查找用户放在搜索框中的ID是否与ID在JSON中,如果是我想返回关于该人的一些信息,让我们说用户的名字和用户名。搜索框结果== JSON结果
注:我100%肯定,我的server.js文件的工作。
请帮助我,我很亲切jQuery的一个新手。
我至今;
$(document).ready(function(){
$("#id_of_the_search_box_button").click(function (any_function_variable_name) {
$.ajax({
type: 'GET',
url: '/url_of_the_related_server_side',
dataType: 'json'
})
.done(function(data){
console.log(data);
alert(JSON.stringify(data));
data.forEach(function (any_function_variable_name) {
if(any_function_variable_name.search_box_ID == any_function_variable_name.name_in_the_JSON_file_for_one_of_the_searched_ID){
$userElement = $("<li>").html("<strong>ID: </strong>" + any_function_variable_name.id); //id is defined in JSON and I was able to use in another part of the jquery
$userBlock = $("<ul>")
.append(
$("<li>").html("<strong>Name: </strong>" + any_function_variable_name.name) //name of the user
)
.append(
$userElement
)
.append(
$("<li>").html("<strong>User User Name: </strong>" + any_function_variable_name.userName)
);
$any_function_variable_nameBlock.insertAfter("#search");
}
});
});})})
注意:我不知道是否可以右括号括起来。
在此先感谢!
你能提供你的json文件的结构吗?样本数据将帮助... –
[{... “ID”:37595960858, “in_reply_to_screen_name”:空, “用户”:{ \t “ID”:2384451, \t “ID_STR”: “238678” , \t “名”: “我多加”, \t “SCREEN_NAME”: “IDoga”, ... “ID”:65739068412, “in_reply_to_screen_name”:空, “用户”:{ \t “ID”:2878009, \t “ID_STR”: “235678”, \t “名”: “乔恩·多伊”, \t“USER_NAME”:“JD”, ... }] 用户在搜索框中应该匹配与一个不是在用户写入的ID。 –
PS:我不使用表达 –