2016-05-11 15 views
2

私はオンラインストアのために構築しているjQuery/JSONベースのライブ検索で数日間この問題を抱えてきました。オープンソースのTipue Dropを使用してテストJSONを使用しているときに結果を得ることはできますが、それを読み込むことはできません。私はJSONファイルhereを持っていて、以下のjavascriptを使って、Tipue Dropを使って名前と説明フィールドを読み込みました。どのように私はこれを解決することができますどのようなアドバイスに感謝!JSONとjQueryライブ検索

/* 
 
Tipue drop 5.0.2 
 
Copyright (c) 2015 Tipue 
 
Tipue drop is released under the MIT License 
 
http://www.tipue.com/drop 
 
*/ 
 

 

 
(function($) { 
 

 
    $.fn.tipuedrop = function(options) { 
 

 
      var set = $.extend({ 
 
      
 
       'show'     : 3, 
 
       'speed'     : 300, 
 
       'newWindow'    : false, 
 
       'mode'     : 'static', 
 
       'contentLocation'  : 'tipuedrop/tipuedrop_content.json' 
 
      
 
      }, options); 
 
      
 
      return this.each(function() { 
 
      
 
       var tipuedrop_in = { 
 
        pages: [] 
 
       }; 
 
       $.ajaxSetup({ 
 
        async: false 
 
       }); 
 
       
 
       if (set.mode == 'json') 
 
       { 
 
        $.getJSON(set.contentLocation) 
 
         .done(function(json) 
 
         { 
 
           tipuedrop_in = $.extend({}, json); 
 
         }); 
 
       }    
 
       
 
       if (set.mode == 'static') 
 
       { 
 
        tipuedrop_in = $.extend({}, tipuedrop); 
 
       } 
 

 
       $(this).keyup(function(event) 
 
       { 
 
        getTipuedrop($(this)); 
 
       });    
 
       
 
       function getTipuedrop($obj) 
 
       { 
 
        if ($obj.val()) 
 
        { 
 
         var c = 0; 
 
         for (var i = 0; i < tipuedrop_in.pages.length; i++) 
 
         { 
 
           var pat = new RegExp($obj.val(), 'i'); 
 
           if ((tipuedrop_in.pages[i].name.search(pat) != -1 || tipuedrop_in.pages[i].description.search(pat) != -1) && c < set.show) 
 
           { 
 
            if (c == 0) 
 
            { 
 
             var out = '<div class="tipue_drop_box"><div id="tipue_drop_wrapper">';  
 
            } 
 
            out += '<a href="' + tipuedrop_in.pages[i].id + '"'; 
 
            if (set.newWindow) 
 
            { 
 
             out += ' target="_blank"'; 
 
            } 
 
            out += '><div class="tipue_drop_item"><div class="tipue_drop_left"><img src="' + tipuedrop_in.pages[i].id + '" class="tipue_drop_image"></div><div class="tipue_drop_right">' + tipuedrop_in.pages[i].name + '</div></div></a>'; 
 
            c++; 
 
           } 
 
         } 
 
         if (c != 0) 
 
         { 
 
           out += '</div></div>';    
 
           $('#tipue_drop_content').html(out); 
 
           $('#tipue_drop_content').fadeIn(set.speed); 
 
         } 
 
        } 
 
        else 
 
        { 
 
         $('#tipue_drop_content').fadeOut(set.speed); 
 
        } 
 
       } 
 
       
 
       $('html').click(function() 
 
       { 
 
        $('#tipue_drop_content').fadeOut(set.speed); 
 
       }); 
 
      
 
      }); 
 
    }; 
 
     
 
})(jQuery);
<script> 
 
    $(document).ready(function() { 
 
     $('#tipue_drop_input').tipuedrop({ 
 
      'mode': 'json', 
 
      'contentLocation': 'http://barbarostudios.com/clientpreviews/lungavita2/api/search.php' 
 
     }); 
 
    }); 
 
</script>

+1

コンソール(F12)にエラーがありますか? –

+0

はい:3tipuedrop.js:60 Uncaught TypeError:未定義の属性 'search'を読み取ることができません –

+0

'tipedrop_in.pages [i] .name.search(pat)'を 'tipuedrop_in.pages [i] .titleに変更したようです.search(pat) '60行目... JSONファイルのどこにでも' title 'は表示されませんが、 'name'が表示されます。変更の具体的な理由は何ですか? –

答えて

-1

これは私のために働いたライブ検索方法です。

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> 

Script.js

$('#search').keyup(function(){ 
var searchField = $('#search').val(); 
var myExp = new RegExp(searchField, 'i'); 
$.getJSON('data.json', function(data){ 
var output = '<ul class="searchresult">'; 
$.each(data, function(key, val){ 
if((val.name.search(myExp) != -1) || (val.bio.search(myExp) != -1)) { 
output +='<li>'; 
output +='<h2>' + val.name + '</h2>'; 
output +='<img src="images/' + val.shortname + '_tn.jpg" alt="'+ val.name +'" />'; 
output +='<p>' + val.bio + '</p>'; 
output +='</li>'; 
} 
}); 
output += '</ul>'; 
$('#update').html(output); 
}); 
}); 

mystyle.css

body { 
background: #DAD7C7; 
} 

#searcharea { 
margin: 0 auto; 
text-align: center; 
background: #BF996B; 
padding: 10px; 
width: 30%; 
-webkit-border-radius: 10px 10px 0 0; 
-moz-border-radius: 10px 10px 0 0; 
border-radius: 10px 10px 0 0; 
} 

#searcharea label { 
font: bold 1.3em Arial; 
text-transform: uppercase; 
padding-bottom: 5px; 
color: #A61C1C; 
} 

#searcharea p { 
margin: 0; 
line-height: 1em; 
padding-bottom: 5px; 
} 

#searcharea input { 
width: 80%; 
text-align: center; 
} 

#update { 
font-family: Georgia, "Times New Roman", Times, serif; 
width: 70%; 
margin: 0 auto; 
border-top: 1px dotted #CCC; 
} 

#update ul { 
list-style: none; 
margin: 0; 
padding: 0; 
} 

#update ul li { 
width: 100%; 
padding: 0 20px; 
background: #EEE; 
padding-bottom: 10px; 
height: 110px; 
overflow: hidden; 
border-bottom: 1px dotted #CCC; 
-webkit-animation: myanim 1s; 
-moz-appearance-animation: myanim 1s; 
-o-animation: myanim 1s; 
animation: myanim 1s; 

-webkit-transition: height 0.3s ease-out; 
-moz-appearance-transition: height 0.3s ease-out; 
-o-transition: height 0.3s ease-out; 
transition: height 0.3s ease-out; 
} 

#update li:hover { 
background: #FFFCE5; 
min-height: 110px; 
height: 250px; 
overflow: visible; 
} 

#update h2 { 
margin: 0; 
padding: 0; 
font-size: 1.2em; 
padding-bottom: 5px; 
padding-top: 20px; 
font-family: Arial, Helvetica, sans-serif; 
color: #BF5841; 
border-bottom: 1px dotted #CCC; 
margin-bottom: 10px; 
} 

#update img { 
float: left; 
width: 80px; 
margin-right: 10px; 
-webkit-border-radius: 10px; 
-moz-border-radius: 10px; 
border-radius: 10px; 
} 

@-webkit-keyframes myanim { 
0% { 
opacity: 0.3; 
} 
100% { 
opacity: 1.0; 
} 
} 
@-o-keyframes myanim { 
0% { 
opacity: 0.3; 
} 
100% { 
opacity: 1.0; 
} 
} 

@-moz-keyframes myanim { 
0% { 
opacity: 0.3; 
} 
100% { 
opacity: 1.0; 
} 
} 

@keyframes myanim { 
0% { 
opacity: 0.3; 
} 
100% { 
opacity: 1.0; 
} 
} 

[ 
{ 
"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." 
} 
] 

data.jsonあなたはデモhereを見ることができます。

例:

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

以前はそのメソッドを使用していましたが、まだ運がありませんでした。 JSONファイルのサイズがそれと何か関係があるのだろうかと思います。 –

+0

コンソールのエラーをチェックしました –

+0

はい:3tipuedrop.js:60 Uncaught TypeError:未定義のプロパティ 'search'を読み取ることができません –

0

これ、あなたがそのオフセットを取得しようとする前にjQueryオブジェクトは、任意の要素が含まれている場合


私はあなたのエラーを解決するために取得しています一つの方法は、そのチェックですあなたのアイテムのいくつかに未定義のインデックスがあるため、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を引き起こし、スクリプトの残りの部分の処理を停止します(名前と説明のプロパティを特に見ているので)。 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をファイルやデータベースなど)を変更した場合は、そのセットを持たないすべての要素に記述&の名前を動的に追加するように変更することができます(少し変更されたコードと同じです)。

関連する問題