【发布时间】:2016-06-11 08:25:04
【问题描述】:
我有一个读取 JSON 文件的 Node.js 服务器,其中有很多信息。其中之一是一个人的 ID,我的 HTML 页面中有一个搜索框,我想为客户端设置一个 jquery 方法(需要 ajax)来查找用户在搜索框中输入的 ID 是否与JSON 中的 ID,如果我想返回有关该人的一些信息,假设是用户的姓名和用户名。
注意:我 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": null, "user": { "id": 2384451, "id_str": "238678", "name": "Doga我”,“screen_name”:“IDoga”,...“id”:65739068412,“in_reply_to_screen_name”:null,“user”:{“id”:2878009,“id_str”:“235678”,“name”:“ Jon Doe", "user_name": "JD", ... }] 用户在搜索框中输入的 id 应该与不在用户中的 id 匹配。
-
P.S:我不使用快递
-
只是为了确保我正确理解你的问题,
[ { "id": 37595960858, "in_reply_to_screen_name": null, "user": { "id": 2384451, "id_str": "238678", "name": "Doga I", "screen_name": "IDoga" } }, { "id": 65739068412, "in_reply_to_screen_name": null, "user": { "id": 2878009, "id_str": "235678", "name": "Jon Doe", "user_name": "JD" } } ]是你的结构,你想匹配{ id: ...}而不是{ user: { id: ...} }...对吗?
标签: javascript jquery json ajax node.js