2013-07-22 76 views
0

我正在實施搜索功能。我發現它的鏈接工作正常..但一個問題是它沒有設置警報時,有在頁面中找不到匹配項。請告訴我插入該警報的位置。我不調試警報。 我這樣做.match.length()== 0然後我顯示警報,但它不工作,你可以請幫忙嗎?如何設置警報,如果沒有在jquery搜索中找到匹配

這裏是我找到代碼的鏈接。 http://jsbin.com/umodoy/7/edit

var searchIndex = -1; 
var searchTermOld =''; 

$(document).ready(function(){ 
    $('.searchbox').on('change',function(){ 
    if($(this).val()===''){ 
     var selector= "#realTimeContents"; 
    $(selector+' span.match').each(function(){ 
     $(this).replaceWith($(this).html()); 
     }); 
    } 
     searchIndex = -1; 
     $('.searchNext').attr("disabled", "disabled"); 
     $('.searchPrev').attr("disabled", "disabled"); 
    searchTermOld = $(this).val(); 
    }); 
    $('.searchbox').on('keyup',function(){ 

    var selector= "#realTimeContents"; 
    if($(this).val()===''){ 
    $(selector+' span.match').each(function(){ 
     $(this).replaceWith($(this).html()); 
     }); 
    } 
    if($(this).val() !== searchTermOld){ 
    $(selector+' span.match').each(function(){ 
     $(this).replaceWith($(this).html()); 
     }); 
     searchIndex = -1; 
     $('.searchNext').attr("disabled", "disabled"); 
     $('.searchPrev').attr("disabled", "disabled");        
    } 
    }); 
    $('.search').on('click',function(){ 
    if(searchIndex == -1){ 
     var searchTerm = $('.searchbox').val(); 
     searchAndHighlight(searchTerm); 
    } 
    else searchNext(); 
    if($('.match').length >1){ 
     $('.searchNext').removeAttr("disabled"); 
     $('.searchPrev').removeAttr("disabled"); 
    } 
    }); 
    $('.searchNext').on('click',searchNext); 

    $('.searchPrev').on('click',searchPrev); 
}); 

function searchAndHighlight(searchTerm) { 
    if (searchTerm) { 
     var searchTermRegEx, matches; 
     var selector= "#realTimeContents"; 
     $(selector+' span.match').each(function(){ 
     $(this).replaceWith($(this).html()); 
     }); 
     try { 
      searchTermRegEx = new RegExp('('+searchTerm+')', "ig"); 
     } catch (e) { 
      return false; 
     } 
     $('.highlighted').removeClass('highlighted'); 
     matches = $(selector).text().match(searchTermRegEx); 
     if (matches !==null && matches.length > 0) { 
      var txt = $(selector).text().replace(searchTermRegEx, '<span class="match">$1</span>'); 
      $(selector).html(txt); 
      searchIndex++; 
      $('.match:first').addClass('highlighted'); 
      $(document).scrollTop($('.match').eq(searchIndex).offset().top); 

      return true; 
     } 
     return false; 
    } 
    return false; 
} 

function searchNext(){ 
    searchIndex++; 
    if (searchIndex >= $('.match').length) searchIndex = 0; 
    $('.highlighted').removeClass('highlighted'); 
    $('.match').eq(searchIndex).addClass('highlighted'); 
    $(document).scrollTop($('.match').eq(searchIndex).offset().top); 
} 

function searchPrev(){ 
    searchIndex--; 
    if (searchIndex < 0) searchIndex = $('.match').length - 1; 
    $('.highlighted').removeClass('highlighted'); 
    $('.match').eq(searchIndex).addClass('highlighted'); 
    $(document).scrollTop($('.match').eq(searchIndex).offset().top); 
} 

回答

2

工作演示http://jsbin.com/umodoy/29/edit

function searchAndHighlight(searchTerm) { 
    if (searchTerm) { 
     var searchTermRegEx, matches; 
     var selector= "#realTimeContents"; 
     $(selector+' span.match').each(function(){ 
     $(this).replaceWith($(this).html()); 
     }); 
     try { 
      searchTermRegEx = new RegExp('('+searchTerm+')', "ig"); 
     } catch (e) { 
      return false; 
     } 
     $('.highlighted').removeClass('highlighted'); 
     matches = $(selector).text().match(searchTermRegEx); 
     if (matches !==null && matches.length > 0) { 
      var txt = $(selector).text().replace(searchTermRegEx, '<span class="match">$1</span>'); 
      $(selector).html(txt); 
      searchIndex++; 
      $('.match:first').addClass('highlighted'); 
      $(document).scrollTop($('.match').eq(searchIndex).offset().top); 

      return true; 
     }else{ //added this else here 
      alert('not found'); 
     } 
     return false; 
    } 
    return false; 
} 

滾動問題JS

工作演示http://jsbin.com/umodoy/37/edit

替代由該

if($('.match').eq(searchIndex).offset().top > $(window).height()-10){ 
    $(document).scrollTop($('.match').eq(searchIndex).offset().top); 
} 
012滾動碼
+0

只有一個問題,它滾動到很多。如果匹配在同一頁上它也scroll.I認爲它只是滾動時,它的頁面上方或下方 – Rohit

+0

讓我檢查.. –

+0

你首先添加大量數據,然後搜索它滾動太多,它看起來不好,它滾動到文本.. – Rohit

1

Demo here
你可以,如果返回true的結束後添加。

像:

return true; 
    } 
    alert('no matches!'); 
    return false; 

if (matches !==null && matches.length > 0)搜索匹配null不同,並返回true,這使得功能停止。如果該陳述未被滿足/爲true,則可以在此之後放置警報。

+0

滾動問題滾動得太多.. :( – Rohit

+0

@Rohit,你是什麼意思滾動?演示中沒有滾動條,只是提示框中沒有找到匹配項。 – Sergio

+0

實際上,當searvh被發現..如果測試發現在字體(相同的頁面),它應該只去upto焦點文本 – Rohit

1

只需檢查searchAndHighlight的返回值,如果其虛假顯示警報。

if(!searchAndHighlight(searchTerm)) 
    alert('No Matches Found.'); 
相關問題