2012-04-17 3 views
0

다음에 검색을 만들고 자바 스크립트로 모든 버튼을 검색 할 수있는 방법이 있습니까? 이 다음 버튼 자바 스크립트 검색

<form action="" method="get" onsubmit="selectString(document.getElementById('text'), document.getElementById('search').value);return false;">    
     <legend> 
      <font face="verdana">Zoekveld</font face> 
     </legend> 
     <input id="search" name="search" type="text" size="75"> 
     <input type="submit" value="Zoeken"> 
</form> 

<form name=form1 method="get"> 
     <legend> 
      <font face="Verdana">Vind:</font face> 
     </legend> 
     <textarea id="text" cols="54" rows="20"></textarea> 
</form> 

<script type="text/javascript"> 
    function setSelectionRange(input, selectionStart, selectionEnd) 
    { 
     input.focus(); 
     input.setSelectionRange(selectionStart, selectionEnd); 
    } 
    function selectString (input, string) 
    { 
     var match = new RegExp(string, "g").exec(input.value); 
     if (match) 
    { 
    setSelectionRange (input, match.index, match.index + match[0].length); 
    } 
    } 
</script> 

이 스크립트의 문제

그것이 첫 번째 일치하는 항목을 찾을 수 있다는 것입니다 ... 지금까지 내 스크립트입니다 .. .

답변

0

RegExp i를 저장하십시오. n 각 함수 실행마다 새 변수를 만드는 대신 변수를 사용하면 카운터 속성 lastIndexexec()으로 유지됩니다.

var r = /^$/; 
function setSelector(string) { 
    r = new RegExp(string, "i"); 
} 
function selectNextMatch(input) { 
    var match = r.exec(input.value); 
    if (match) 
     setSelectionRange(...); 
    else 
     alert("Nothing found"); 
} 
<input type="text" size="75" onchange="setSelector(this.value);"> 
<input type="button" value="Zoeken" onclick="selectNextMatch(document.getElementById('text'));"> 
<textarea id="text" cols="54" rows="20"></textarea> 
+0

그래서 난 그냥으로 변경하는 경우 :. VAR 일치 = 새 정규식 (문자열, "I")는 lastIndex (input.value); 작동해야합니까? 그러나 (javascript에서 그런 멍청한 녀석 인 것에 대해 유감스럽게 생각합니다 ... (나를 위해 제 스크립트를 바꿀 수 있습니까? 아니면 어떻게하는지 또는 튜토리얼이나 다른 곳을 얻을 수있는 곳을 말해 주시겠습니까?) – user1339469

+0

예제를 추가했습니다. * 다른 일치 항목을 찾으려고 할 때 * 스크립트가 그것에 달려 있다고 확신하지 않습니다. – Bergi

+0

코드를 변경하려고합니다. 모든 일치 항목을 찾으려고합니다. 지금까지 도움을 준 Thx : – user1339469