지금 내 라이브 검색에 문자를 입력하면 해당 문자가 포함되어 있지만 시작되지 않은 결과가 표시됩니다. 입력 한 문자로 시작하는 결과 만 표시하려면 코드가 필요합니다. 결과가없는 경우 "제안 없음"이 표시되어야하지만 어떻게 든 작동하지 않습니다. 누구든지 고칠 수 있습니까?
function showResult(str) {
if (str.length==0) {
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px";
return;
}
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("livesearch").innerHTML=this.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
}
xmlhttp.open("GET","livesearch.php?q="+str,true);
xmlhttp.send();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div id="sb">
<form>
<input type="text" placeholder="Type ..." onkeyup="showResult(this.value)">
<div id="livesearch"></div>
</form>
</div>
<?php $xmlDoc=new DOMDocument(); $xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL $q=$_GET["q"];
//lookup all links from the xml file if length of q>0 if (strlen($q)>0) { $hint=""; for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
} } }
// Set output to "no suggestion" if no hint was found // or to the correct values if ($hint=="") { $response="no suggestion"; } else { $response=$hint; }
//output the response echo $response; ?>
<pages> <link> <title>Example</title> <url>https://www.w3schools.com/tags/tag_a.asp</url> </link> <link> <title>Example</title> <url>https://www.w3schools.com/tags/tag_br.asp</url> </link> </pages>
-
답변 # 1
관련 자료
- batch file - 자신의 첫 글자 디렉토리로 폴더 이동
- r - 모든 변수 이름의 첫 글자를 잘라내십시오
- c++ - 일부 입력의 첫 글자를 수락하고 특정 목록 내에 있는지 확인하는 방법이 있습니까?
- regex - PHP를 사용하여 문자열의 특정 첫 글자를 대문자로 변경하는 방법은 무엇입니까?
- javascript - 첫 글자 반복시 이름의 서식
- 파이썬에서 첫 글자 대신 배열에서 전체 단어를 얻는 방법
- ios - 텍스트 필드 첫 글자는 항상 소문자로 표시됩니다
- R - 아르 자형 - str_view ()를 사용하여 첫 글자와 끝 글자가 같은 단어 찾기
- ms access - 단일 텍스트 상자에서 각 단어의 첫 글자를 얻는 방법
- php - 문자 전환 사례 - 공백 뒤뿐만 아니라 슬래시 뒤의 첫 글자 대문자
- javascript - 문자열에서 특정 단어의 첫 글자 변경
- javascript - ReactJs setState는 첫 글자를 건너 뜁니다
- dart - Flutter 및 Firestore를 사용하여 Google 로그인 중 사용자의 첫 번째 표시 이름을 얻는 방법은 무엇입니까?
- html - iOS Safari가 첫 글자를 대문자로 입력하지 못하도록 방지
- ruby - 각 단어의 첫 글자를 얻는 방법
- java - TabLayout에서 첫 글자를 제외한 모든 글자를 소문자로 바꾸는 방법은 무엇입니까?
- Java - 자바 - 모든 줄에 첫 글자가 빠짐
- sed - (macos 터미널) 정규식 일치의 첫 글자를 대문자로 변경
- regex - javascript 함수 - 함수의 입력 str에서 _ 다음에 오는 첫 글자와 모든 글자를 대문자로 표시
항목을 검색하는 조건부 구문을 확인하십시오.
이것은 수색하고 있습니다
$q
항목의 어느 부분에서든 다음으로 시작하는 경우에만 검색하면됩니다.$q
이므로 구문을 변경해야합니다.이제 처음에 일치하는지 검색합니다 (위치 === 0). 당신은 또한 내가
strtolower()
두 비교 값 모두에서 이것은strpos()
대소 문자를 구분하며stristr()
그렇지 않습니다.이제이 줄을 변경하여 효과가 있는지 알려주시겠습니까?