2012-09-16 5 views
3

에서 검색 텍스트 나는이 시도 :jQuery를이 : 문자열

form['comment'] = '<blockquote>test</blockquote><a href="#">test</a> <Your reply here>'; 

if($(form['comment']).search('<Your reply here>')) 
    alert(1) 
else 
    alert(0) 

을하지만이 오류가 : 당신이 원하는 무엇

if(form['comment'].value.search('<Вашият отговор тук>') != -1) 
    alert(1) 
else 
    alert(0) 
+0

jquery js 파일을 포함 시켰습니까? – rekire

+0

예, jquery.js 파일을 포함합니다. – Defense

+1

'search'는 jQuery 객체가 아닌 String 메소드입니다. – xdazz

답변

3

$(form['comment']).text().search()입니다 :

TypeError: $(form.comment).search is not a function

내가이 문제를 해결을

so :

form = []; 
form['comment'] = '<blockquote>test</blockquote><a href="#">test</a> <Your reply here>'; 

if(form['comment'].search('<Your reply here>') != -1) 
    alert(1) 
else 
    alert(0)​ 

Here's a working example.

+0

항상 경고 1입니다. – Defense

+0

이 예에서는 항상 텍스트에 "<여기에 답장>"이 있기 때문에 항상 1로 경고합니다. –

+0

그러나 <사용자의 답장> 양식 텍스트를 다시 경고 1 제거 : – Defense

0

문자열을 다른 문자열과 비교하면 jQuery!가 필요하지 않음을 알 수 있습니다.

var comment = "<blockquote>test</blockquote><a href="#">test</a> <Your reply here>", 
    search = "<Your reply here>"; 


comment.indexOf(search) != -1; // indexOf returns -1 when search does not match 
/<Your reply here>/.test(comment); // returns true when search is in comment.