Javascript JSON和jQuery实时搜索

Javascript JSON和jQuery实时搜索,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我为一家在线商店构建的基于jQuery/JSON的实时搜索已经在这个问题上纠缠了好几天了。使用开源,我可以在使用他们的测试JSON时获得结果,但似乎无法让它读取我的。我有一个JSON文件,正在使用下面的javascript尝试使用Tipue Drop读取名称和描述字段。任何关于如何解决此问题的建议都将不胜感激 /* Tipue下降5.0.2 版权所有(c)2015 Tipue Tipue drop是根据麻省理工学院许可证发布的 http://www.tipue.com/drop */ (函数(

我为一家在线商店构建的基于jQuery/JSON的实时搜索已经在这个问题上纠缠了好几天了。使用开源,我可以在使用他们的测试JSON时获得结果,但似乎无法让它读取我的。我有一个JSON文件,正在使用下面的javascript尝试使用Tipue Drop读取名称和描述字段。任何关于如何解决此问题的建议都将不胜感激

/*
Tipue下降5.0.2
版权所有(c)2015 Tipue
Tipue drop是根据麻省理工学院许可证发布的
http://www.tipue.com/drop
*/
(函数($){
$.fn.tipuedrop=函数(选项){
变量集=$.extend({
"秀":3,,
“速度”:300,
“newWindow”:false,
“模式”:“静态”,
“contentLocation”:“tipuedrop/tipuedrop_content.json”
},选项);
返回此值。每个(函数(){
变量tipuedrop_in={
页码:[]
};
$.ajaxSetup({
异步:false
});
如果(set.mode==“json”)
{
$.getJSON(set.contentLocation)
.done(函数(json)
{
tipuedrop_in=$.extend({},json);
});
}               
如果(set.mode==“静态”)
{
tipuedrop_in=$.extend({},tipuedrop);
}
$(此).keyup(函数(事件)
{
getTipuedrop($(this));
});               
函数getTipuedrop($obj)
{
如果($obj.val())
{
var c=0;
对于(var i=0;i

$(文档).ready(函数(){
$('#tipue_drop_input')。tipuedrop({
'mode':'json',
“contentLocation”:”http://barbarostudios.com/clientpreviews/lungavita2/api/search.php'
});
});

这是一种适合我的实时搜索方法

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Live Search</title>
<link rel="stylesheet" href="mystyle.css" />
</head>
<body>
<div id="searcharea">
<label for="search">live search</label>
<p>Enter the name or info about a speaker</p>
<input type="search" name="search" id="search" placeholder="name or info" />
</div>
<div id="update"></div>
<script src="jquery.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</body>
</html>
data.json

[
{
"name":"Barot Bellingham",
"shortname":"Barot_Bellingham",
"reknown":"Royal Academy of Painting and Sculpture",
"bio":"Barot has just finished his final year at The Royal Academy of Painting and Sculpture, where he excelled in glass etching paintings and portraiture. Hailed as one of the most diverse artists of his generation, Barot is equally as skilled with watercolors as he is with oils, and is just as well-balanced in different subject areas. Barot's collection entitled "The Un-Collection" will adorn the walls of Gilbert Hall, depicting his range of skills and sensibilities - all of them, uniquely Barot, yet undeniably different"
},
{
"name":"Jonathan G. Ferrar II",
"shortname":"Jonathan_Ferrar",
"reknown":"Artist to Watch in 2012",
"bio":"The Artist to Watch in 2012 by the London Review, Johnathan has already sold one of the highest priced-commissions paid to an art student, ever on record. The piece, entitled Gratitude Resort, a work in oil and mixed media, was sold for $750,000 and Jonathan donated all the proceeds to Art for Peace, an organization that provides college art scholarships for creative children in developing nations"
},
{
"name":"Hillary Hewitt Goldwynn-Post",
"shortname":"Hillary_Goldwynn",
"reknown":"New York University",
"bio":"Hillary is a sophomore art sculpture student at New York University, and has already won all the major international prizes for new sculptors, including the Divinity Circle, the International Sculptor's Medal, and the Academy of Paris Award. Hillary's CAC exhibit features 25 abstract watercolor paintings that contain only water images including waves, deep sea, and river."
},
{
"name":"Hassum Harrod",
"shortname":"Hassum_Harrod",
"reknown":"Art College in New Dehli",
"bio":"The Art College in New Dehli has sponsored Hassum on scholarship for his entire undergraduate career at the university, seeing great promise in his contemporary paintings of landscapes - that use equal parts muted and vibrant tones, and are almost a contradiction in art. Hassum will be speaking on "The use and absence of color in modern art" during Thursday's agenda."
},
{
"name":"Jennifer Jerome",
"shortname":"Jennifer_Jerome",
"reknown":"New Orleans, LA",
"bio":"A native of New Orleans, much of Jennifer's work has centered around abstract images that depict flooding and rebuilding, having grown up as a teenager in the post-flood years. Despite the sadness of devastation and lives lost, Jennifer's work also depicts the hope and togetherness of a community that has persevered. Jennifer's exhibit will be discussed during Tuesday's Water in Art theme."
},
{
"name":"LaVonne L. LaRue",
"shortname":"LaVonne_LaRue",
"reknown":"Chicago, IL",
"bio":"LaVonne's giant-sized paintings all around Chicago tell the story of love, nature, and conservation - themes that are central to her heart. LaVonne will share her love and skill of graffiti art on Monday's schedule, as she starts the painting of a 20-foot high wall in the Rousseau Room of Hotel Contempo in front of a standing-room only audience in Art in Unexpected Places."
},
{
"name":"Constance Olivia Smith",
"shortname":"Constance_Smith",
"reknown":"Fullerton-Brighton-Norwell Award",
"bio":"Constance received the Fullerton-Brighton-Norwell Award for Modern Art for her mixed-media image of a tree of life, with jewel-adorned branches depicting the arms of humanity, and precious gemstone-decorated leaves representing the spouting buds of togetherness. The daughter of a New York jeweler, Constance has been salvaging the discarded remnants of her father's jewelry-making since she was five years old, and won the New York State Fair grand prize at the age of 8 years old for a gem-adorned painting of the Manhattan Bridge."
},
{
"name":"Riley Rudolph Rewington",
"shortname":"Riley_Rewington",
"reknown":"Roux Academy of Art, Media, and Design",
"bio":"A first-year student at the Roux Academy of Art, Media, and Design, Riley is already changing the face of modern art at the university. Riley's exquisite abstract pieces have no intention of ever being understood, but instead beg the viewer to dream, create, pretend, and envision with their mind's eye. Riley will be speaking on the "Art of Abstract" during Thursday's schedule"
},
{
"name":"Xhou Ta",
"shortname":"Xhou_Ta",
"reknown":"China International Art University",
"bio":"A senior at the China International Art University, Xhou has become well-known for his miniature sculptures, often the size of a rice granule, that are displayed by rear projection of microscope images on canvas. Xhou will discuss the art and science behind his incredibly detailed works of art."
}
]
你可以看到演示


解决错误的一种方法是,在尝试获取jQuery对象的偏移量之前,检查该对象是否包含任何元素:

例如:

var nav = $('.content-nav');
if (nav.length) {
  var contentNav = nav.offset().top;
  ...continue to set up the menu
}

这是一种适合我的实时搜索方法

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Live Search</title>
<link rel="stylesheet" href="mystyle.css" />
</head>
<body>
<div id="searcharea">
<label for="search">live search</label>
<p>Enter the name or info about a speaker</p>
<input type="search" name="search" id="search" placeholder="name or info" />
</div>
<div id="update"></div>
<script src="jquery.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</body>
</html>
data.json

[
{
"name":"Barot Bellingham",
"shortname":"Barot_Bellingham",
"reknown":"Royal Academy of Painting and Sculpture",
"bio":"Barot has just finished his final year at The Royal Academy of Painting and Sculpture, where he excelled in glass etching paintings and portraiture. Hailed as one of the most diverse artists of his generation, Barot is equally as skilled with watercolors as he is with oils, and is just as well-balanced in different subject areas. Barot's collection entitled "The Un-Collection" will adorn the walls of Gilbert Hall, depicting his range of skills and sensibilities - all of them, uniquely Barot, yet undeniably different"
},
{
"name":"Jonathan G. Ferrar II",
"shortname":"Jonathan_Ferrar",
"reknown":"Artist to Watch in 2012",
"bio":"The Artist to Watch in 2012 by the London Review, Johnathan has already sold one of the highest priced-commissions paid to an art student, ever on record. The piece, entitled Gratitude Resort, a work in oil and mixed media, was sold for $750,000 and Jonathan donated all the proceeds to Art for Peace, an organization that provides college art scholarships for creative children in developing nations"
},
{
"name":"Hillary Hewitt Goldwynn-Post",
"shortname":"Hillary_Goldwynn",
"reknown":"New York University",
"bio":"Hillary is a sophomore art sculpture student at New York University, and has already won all the major international prizes for new sculptors, including the Divinity Circle, the International Sculptor's Medal, and the Academy of Paris Award. Hillary's CAC exhibit features 25 abstract watercolor paintings that contain only water images including waves, deep sea, and river."
},
{
"name":"Hassum Harrod",
"shortname":"Hassum_Harrod",
"reknown":"Art College in New Dehli",
"bio":"The Art College in New Dehli has sponsored Hassum on scholarship for his entire undergraduate career at the university, seeing great promise in his contemporary paintings of landscapes - that use equal parts muted and vibrant tones, and are almost a contradiction in art. Hassum will be speaking on "The use and absence of color in modern art" during Thursday's agenda."
},
{
"name":"Jennifer Jerome",
"shortname":"Jennifer_Jerome",
"reknown":"New Orleans, LA",
"bio":"A native of New Orleans, much of Jennifer's work has centered around abstract images that depict flooding and rebuilding, having grown up as a teenager in the post-flood years. Despite the sadness of devastation and lives lost, Jennifer's work also depicts the hope and togetherness of a community that has persevered. Jennifer's exhibit will be discussed during Tuesday's Water in Art theme."
},
{
"name":"LaVonne L. LaRue",
"shortname":"LaVonne_LaRue",
"reknown":"Chicago, IL",
"bio":"LaVonne's giant-sized paintings all around Chicago tell the story of love, nature, and conservation - themes that are central to her heart. LaVonne will share her love and skill of graffiti art on Monday's schedule, as she starts the painting of a 20-foot high wall in the Rousseau Room of Hotel Contempo in front of a standing-room only audience in Art in Unexpected Places."
},
{
"name":"Constance Olivia Smith",
"shortname":"Constance_Smith",
"reknown":"Fullerton-Brighton-Norwell Award",
"bio":"Constance received the Fullerton-Brighton-Norwell Award for Modern Art for her mixed-media image of a tree of life, with jewel-adorned branches depicting the arms of humanity, and precious gemstone-decorated leaves representing the spouting buds of togetherness. The daughter of a New York jeweler, Constance has been salvaging the discarded remnants of her father's jewelry-making since she was five years old, and won the New York State Fair grand prize at the age of 8 years old for a gem-adorned painting of the Manhattan Bridge."
},
{
"name":"Riley Rudolph Rewington",
"shortname":"Riley_Rewington",
"reknown":"Roux Academy of Art, Media, and Design",
"bio":"A first-year student at the Roux Academy of Art, Media, and Design, Riley is already changing the face of modern art at the university. Riley's exquisite abstract pieces have no intention of ever being understood, but instead beg the viewer to dream, create, pretend, and envision with their mind's eye. Riley will be speaking on the "Art of Abstract" during Thursday's schedule"
},
{
"name":"Xhou Ta",
"shortname":"Xhou_Ta",
"reknown":"China International Art University",
"bio":"A senior at the China International Art University, Xhou has become well-known for his miniature sculptures, often the size of a rice granule, that are displayed by rear projection of microscope images on canvas. Xhou will discuss the art and science behind his incredibly detailed works of art."
}
]
你可以看到演示


解决错误的一种方法是,在尝试获取jQuery对象的偏移量之前,检查该对象是否包含任何元素:

例如:

var nav = $('.content-nav');
if (nav.length) {
  var contentNav = nav.offset().top;
  ...continue to set up the menu
}

在我看来,这仍然像是JavaScript错误,因为您的某些项上的索引未定义。其中一个没有名称(dae04696-2acb-4972-a471-1b873e2a8d4f),而且一整组都没有描述:

159e522f-10fd-4577-ad3b-866da83d0054
19b63864-9566-4264-9510-821c9d020f74
2417fcae-7ce2-4539-a334-c88e0a81b8a9
26a014ac-b23b-4bc2-9b19-b754e875b5f1
2a9f8d42-0dbc-470a-a59b-4cf1aff0a0ed
38c389e2-6926-425d-baa0-0760d63926c1
3dc823a7-9bac-4290-9aae-757480ffe5c1
3e2fbcec-e35d-4f75-94c3-e4de71f4b428
4aba2175-e1cc-4e12-9135-9cec3e4f8914
54b8cba1-3c4b-4bf5-bf6c-b7bd1cfb25b4
5b2867c1-d077-4deb-9d8e-890d3afd5163
5b2d8d5a-1d94-499c-9205-40888472a3f3
5b7dd41a-59cd-49d1-a06f-a9b2a90e516d
5bf00622-33e1-4bed-a00b-0f14826cd2b7
6164bb51-0d0e-4eb1-81bb-cef64bca56cb
6376edc3-faba-4018-8bc2-8b3e0d9fcb48
6d618850-dca0-495f-92ed-f8b86578b063
740b265a-fd38-4bcf-8e68-ab14e8266543
79728b83-6365-4a00-8634-10656081a574
7b093823-599e-481c-8cbe-f1cfab3674e6
7ec54482-4e6e-470b-ac19-6b9cee2b7db3
9552f568-b7bd-44f2-b144-f3773ae0d097
9860c5a8-2a04-4176-95bc-762895a077e3
9a418680-cb24-4f2c-a56f-cb555edd9db4
9cad6135-3d87-4fda-988d-9ec3f7958f31
a676471c-8633-4bd4-a801-3066d59c2c00
ac20c6f3-0627-4b7b-85cb-7960c0935d3c
b09a2984-a868-47e4-84de-177572fd2b16
bc7b3be8-c388-4e1a-a884-8a899ff13889
bcff6f69-eb86-468d-be40-90e21f477f56
beddc257-4490-41e4-ba1d-088f2bd75108
c035feaa-23ae-4e60-948d-8e8fd9b6d314
c5f2083a-c5b0-4c98-934c-f7f006230285
d4ddcb79-3e0e-4958-a2de-260c05d0596c
d5447be8-8b92-473e-9df0-7d0e1a0b6ed1
d7e25d21-3e8a-4ddc-832b-e5a7939b1e75
dae04696-2acb-4972-a471-1b873e2a8d4f
e08cb73e-1060-4a28-abe0-2beba19f8407
fb464d67-7406-48d7-98d9-2c094f219079
这会导致
tipuedrop
在页面中循环时失败,并停止处理脚本的其余部分(因为您专门查看的是name和description属性)

如果您的PHP页面正在从JSON文件中提取JSON,您可以使用它静态修改JSON文件以修复它:

<?php
    $json = json_decode(file_get_contents('json.json'));
    foreach($json->pages as &$page){
        if(!isset($page->description)){
            $page->description = "";
        }
        if(!isset($page->name)){
            $page->name = "";
        }
    }
    file_put_contents('json.json', json_encode($json));
?>


或者,如果您的PHP页面将信息拉到其他位置(txt文件、数据库或其他东西),您可以将其更改为动态地为没有该设置的每个元素添加一个描述和名称(代码基本相同,只是修改了一点),并回显动态更改的代码。

在我看来,这仍然像是JavaScript错误,因为您的某些项目上的索引未定义。其中一个没有名称(dae04696-2acb-4972-a471-1b873e2a8d4f),而且一整组都没有描述:

159e522f-10fd-4577-ad3b-866da83d0054
19b63864-9566-4264-9510-821c9d020f74
2417fcae-7ce2-4539-a334-c88e0a81b8a9
26a014ac-b23b-4bc2-9b19-b754e875b5f1
2a9f8d42-0dbc-470a-a59b-4cf1aff0a0ed
38c389e2-6926-425d-baa0-0760d63926c1
3dc823a7-9bac-4290-9aae-757480ffe5c1
3e2fbcec-e35d-4f75-94c3-e4de71f4b428
4aba2175-e1cc-4e12-9135-9cec3e4f8914
54b8cba1-3c4b-4bf5-bf6c-b7bd1cfb25b4
5b2867c1-d077-4deb-9d8e-890d3afd5163
5b2d8d5a-1d94-499c-9205-40888472a3f3
5b7dd41a-59cd-49d1-a06f-a9b2a90e516d
5bf00622-33e1-4bed-a00b-0f14826cd2b7
6164bb51-0d0e-4eb1-81bb-cef64bca56cb
6376edc3-faba-4018-8bc2-8b3e0d9fcb48
6d618850-dca0-495f-92ed-f8b86578b063
740b265a-fd38-4bcf-8e68-ab14e8266543
79728b83-6365-4a00-8634-10656081a574
7b093823-599e-481c-8cbe-f1cfab3674e6
7ec54482-4e6e-470b-ac19-6b9cee2b7db3
9552f568-b7bd-44f2-b144-f3773ae0d097
9860c5a8-2a04-4176-95bc-762895a077e3
9a418680-cb24-4f2c-a56f-cb555edd9db4
9cad6135-3d87-4fda-988d-9ec3f7958f31
a676471c-8633-4bd4-a801-3066d59c2c00
ac20c6f3-0627-4b7b-85cb-7960c0935d3c
b09a2984-a868-47e4-84de-177572fd2b16
bc7b3be8-c388-4e1a-a884-8a899ff13889
bcff6f69-eb86-468d-be40-90e21f477f56
beddc257-4490-41e4-ba1d-088f2bd75108
c035feaa-23ae-4e60-948d-8e8fd9b6d314
c5f2083a-c5b0-4c98-934c-f7f006230285
d4ddcb79-3e0e-4958-a2de-260c05d0596c
d5447be8-8b92-473e-9df0-7d0e1a0b6ed1
d7e25d21-3e8a-4ddc-832b-e5a7939b1e75
dae04696-2acb-4972-a471-1b873e2a8d4f
e08cb73e-1060-4a28-abe0-2beba19f8407
fb464d67-7406-48d7-98d9-2c094f219079
这会导致
tipuedrop
在页面中循环时失败,并停止处理脚本的其余部分(因为您专门查看的是name和description属性)

如果您的PHP页面正在从JSON文件中提取JSON,您可以使用它静态修改JSON文件以修复它:

<?php
    $json = json_decode(file_get_contents('json.json'));
    foreach($json->pages as &$page){
        if(!isset($page->description)){
            $page->description = "";
        }
        if(!isset($page->name)){
            $page->name = "";
        }
    }
    file_put_contents('json.json', json_encode($json));
?>


或者,如果您的PHP页面将信息拉到其他位置(txt文件、数据库或其他内容),您可以将其更改为动态添加一个描述和名称到未设置该描述和名称的每个元素(代码大致相同,只是修改了一点),并回显动态更改的代码。

您的控制台(F1)中是否有任何错误