2016-07-18 229 views
0

遺漏的類型錯誤:$(本).search不是一個函數

$(document).ready(function(){ 
    $('#caption').on('keypress', function() { 
      var n = $(this).search('#'); 
      if(n != "-1"){ 
       window.alert("There's a hash"); 
      }else{ 
       window.alert("There's not a hash"); 
      } 


    }); 
}); 
+1

你需要'find'! –

+0

什麼是'#標題',以什麼方式搜索'#'?它是價值,HTML還是屬性? – 4castle

+0

#是textarea的值 – Linuxe

回答

2

search爲字符串類型JavaScript方法。

所以,如果你想使用search,你行var n = $(this).search('#');應視#caption元素的標籤改爲var n = $(this).val().search('#');var n = $(this).text().search('#');

0

您可以使用indexof(),如果你想在你輸入的字符搜索#

請記住,search()用於正則表達式。否則indexOf()將會更快。

$(document).ready(function(){ 
    $('#caption').on('keypress', function() { 
     var n = $(this).val(); 
     if(n.indexOf("#") > -1){ 
      window.alert("There's a hash"); 
     }else{ 
      window.alert("There's not a hash"); 
     } 

    }); 
}); 

結果:https://jsfiddle.net/cmedina/3v04fvmb/

相關問題