2013-06-11 2 views
0

첫 번째 이름을 입력 할 입력란이 들어있는 html 파일이 있습니다. 클릭하면 XML 파일을로드하여 일치하는 항목이 있는지 확인하는 JavaScript 함수를 실행합니다. 일치하는 항목이 있으면 일치하는 연락처 정보가있는 테이블에 표시됩니다. 나는 이걸 잘 할 수있어.자바 스크립트 XML 검색 - 일치하는 항목이없는 경우 메시지 표시

하지만 일치하는 항목이 없으면 알림을 표시해야합니다. 그것은 "연락처가 없다면"입니다.

내 코드의 for 루프가 innerHTML을 엉망으로 만듭니다.이 코드는 반복적으로 특정 코드를 반복하여 내부의 모든 코드를 구문 분석합니다. 이것이 innerHTML이 루프 외부에있는 이유입니다.

그러나 연락처가없는 경우 테이블이 호출되기 전에 메시지를 표시하려고합니다.

아마도 매우 간단 할 수도 있지만 하루 종일 나를 피했습니다.

Heres는 내 코드 :

<script language="JavaScript" type="text/javascript"> 
function loadXMLDoc(dname) 
{ 
    if (window.XMLHttpRequest) 
    { 
     xhttp=new XMLHttpRequest(); 
    } 
    else 
    { 
     xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xhttp.open("GET",dname,false); 
    xhttp.send(); 
    return xhttp.responseXML; 
} 
function searchXML() 
{ 
    xmlDoc=loadXMLDoc("Contact.xml"); 
    x=xmlDoc.getElementsByTagName("firstname"); 
    input = document.getElementById("input").value; 
    size = input.length; 
    divText = "<table border=1><tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Street</th><th>City</th><th>State</th><th>Postcode</th></tr>"; 
    if (input == null || input == "") 
    { 
     document.getElementById("results").innerHTML= "Please enter a character or name!"; 
     return false; 
    } 
    for (i=0;i<x.length;i++) 
    { 
     startString = firstname.substring(0,size); 
     if (startString.toLowerCase() == input.toLowerCase()) 
     { 
      firstname=xmlDoc.getElementsByTagName("firstname")[i].childNodes[0].nodeValue; 
      lastname=xmlDoc.getElementsByTagName("lastname")[i].childNodes[0].nodeValue; 
      phone=xmlDoc.getElementsByTagName("phone")[i].childNodes[0].nodeValue; 
      street=xmlDoc.getElementsByTagName("street")[i].childNodes[0].nodeValue; 
      city=xmlDoc.getElementsByTagName("city")[i].childNodes[0].nodeValue; 
      state=xmlDoc.getElementsByTagName("state")[i].childNodes[0].nodeValue; 
      postcode=xmlDoc.getElementsByTagName("postcode")[i].childNodes[0].nodeValue; 
      divText = divText + "<tr><td>" + firstname + "</td><td>" + lastname + "</td><td>" + phone + "</td><td>" + street + "</td><td>" + city + "</td><td>" + state + "</td><td>" + postcode + "</td></tr>"; 
      } 
    } 
    //insert does not exist code here 
    document.getElementById("results").innerHTML= "<h2>The contact does not exist.</h2>"; 
    return false; 
    //end insert 
    divText = "<h1>The contact details are:</h1><br />" + divText + "</table>"; 
    document.getElementById("results").innerHTML= divText; 
} 
</script> 

내 HTML 본문 :

<body> 
First Name: <input type="text" name="firstname" id="input"> 
<br /> 
<input type="submit" value="Submit" onClick="searchXML()"> 
<br /> 
<br /> 
<div id="results"> 
</div> 
</body> 

내가 어떤 도움을 주셔서 감사합니다.

감사합니다.

+0

이 –

+0

자바 스크립트 못해 화재 JS 바이올린과 creat 수 있습니다 도움이 될 수 있기를 바랍니다. 나는 그것이 서버에 있지 않기 때문에 그것이라고 생각한다. 나는 로컬에서 같은 문제를 일으켰다. – Curley5959

답변

0

문제가 해결되었습니다. 문제는 연락처가없는 경우에도 테이블 데이터가있는 divText가 표시된다는 것입니다.

for 루프 내에 if else 문이 있습니다. 전체 작업 코드는 아래를 참조하십시오.

<script language="JavaScript" type="text/javascript"> 
function loadXMLDoc(dname) 
{ 
    if (window.XMLHttpRequest) 
    { 
     xhttp=new XMLHttpRequest(); 
    } 
    else 
    { 
     xhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xhttp.open("GET",dname,false); 
    xhttp.send(); 
    return xhttp.responseXML; 
} 
function searchXML() 
{ 
    xmlDoc=loadXMLDoc("Contact.xml"); 
    x=xmlDoc.getElementsByTagName("firstname"); 
    input = document.getElementById("input").value; 
    size = input.length; 
    if (input == null || input == "") 
    { 
     document.getElementById("results").innerHTML= "Please enter a character or name!"; 
     return false; 
    } 
    for (i=0;i<x.length;i++) 
    { 
     startString = firstname.substring(0,size); 
     if (startString.toLowerCase() == input.toLowerCase()) 
     { 
      firstname=xmlDoc.getElementsByTagName("firstname")[i].childNodes[0].nodeValue; 
      lastname=xmlDoc.getElementsByTagName("lastname")[i].childNodes[0].nodeValue; 
      phone=xmlDoc.getElementsByTagName("phone")[i].childNodes[0].nodeValue; 
      street=xmlDoc.getElementsByTagName("street")[i].childNodes[0].nodeValue; 
      city=xmlDoc.getElementsByTagName("city")[i].childNodes[0].nodeValue; 
      state=xmlDoc.getElementsByTagName("state")[i].childNodes[0].nodeValue; 
      postcode=xmlDoc.getElementsByTagName("postcode")[i].childNodes[0].nodeValue; 
      divText = "<h1>The contact details are:</h1><br /><table border=1><tr><th>First Name</th><th>Last Name</th><th>Phone</th><th>Street</th><th>City</th><th>State</th><th>Postcode</th></tr>" + "<tr><td>" + firstname + "</td><td>" + lastname + "</td><td>" + phone + "</td><td>" + street + "</td><td>" + city + "</td><td>" + state + "</td><td>" + postcode + "</td></tr>" + "</table>"; 
      break; 
     } 
     else 
     { 
      divText = "<h2>The contact does not exist.</h2>"; 
     } 
    } 
    document.getElementById("results").innerHTML= divText; 
} 
</script> 

나는이 다른 누군가가

관련 문제