2011-02-02 67 views
0

喜不工作我試圖創建一個JSON數組的JavaScript自定義搜索功能在IE

我的代碼在Chrome和firfox精細定製的搜索,但沒有在IE中。

該代碼的工作原理如下。它循環搜索詞「針」,如果它發現問題中的單詞將它添加到數組中。如果它在同一個問題中發現不同的搜索項,則命中計數器會遞增。

所有的代碼都在這裏,可以運行。

我得到的錯誤是:

Line:24 
Char:3 
Error:qna_questions[..].q is null or not an object 

qna_questions = [ 
{ 'q':'this is a very good question','id':'1'}, 
{ 'q':'i like pllo in the summer','id':'2'}, 
{ 'q':'it rains allot in the summer mushroom','id':'3'}, 
{ 'q':'i love people and straberry cake','id':'4'}, 
{ 'q':'i love people and berry rain','id':'5'}, 
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'}, 
{ 'q':'the dog is fat','id':'7'}, 
]; 

var search_result = new Array(); 
var needle = "dog fat summer allot mushroom"; 
var needle = needle.split(' '); 
for(j = 0; j < qna_questions.length; j++) { 

    for(i =0; i < needle.length; i++) { 

     if(qna_questions[j].q.search(needle[i].toLowerCase()) != -1) { 

      if(search_result[qna_questions[j].id] === undefined) { 
       search_result[qna_questions[j].id] = { 
         'q':qna_questions[j].q, 
         'id':qna_questions[j].id, 
         'hits':0 
         }; 
      } else { 
       search_result[qna_questions[j].id].hits++; 


      } 

     } 
    } 
} 
search_result.sort(function(a,b) { return parseInt(b.hits) - parseInt(a.hits) }); 
for (x in search_result) 
    { 
    alert(search_result[x].q + ": "+search_result[x].hits +"<br />"); 
    } 

回答

2
qna_questions = [ 
{ 'q':'this is a very good question','id':'1'}, 
{ 'q':'i like pllo in the summer','id':'2'}, 
{ 'q':'it rains allot in the summer mushroom','id':'3'}, 
{ 'q':'i love people and straberry cake','id':'4'}, 
{ 'q':'i love people and berry rain','id':'5'}, 
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'}, 
{ 'q':'the dog is fat','id':'7'}, 
]; 

注意在qna_questions結束......在IE瀏覽器,這將是在數組的結尾多了一個空項額外的逗號... 。刪除逗號,你應該很好。

qna_questions = [ 
{ 'q':'this is a very good question','id':'1'}, 
{ 'q':'i like pllo in the summer','id':'2'}, 
{ 'q':'it rains allot in the summer mushroom','id':'3'}, 
{ 'q':'i love people and straberry cake','id':'4'}, 
{ 'q':'i love people and berry rain','id':'5'}, 
{ 'q':'dsff sd fsd rains sdfsd fsd ','id':'6'}, 
{ 'q':'the dog is fat','id':'7'} 
];