2016-07-18 156 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/

相关问题