두 파일 : xml_database.xml
및 login.html
이 있습니다. 이 login.html
내 HTML입니다 : xml/javascript - 노드가 생성되지 않았습니다.
<h2>Login:</h2>
Username: <input type="text" id="login_username"> <input type="button" value="Sign in!" id="sign_in"><br><br><hr>
<h2>Create an account:</h2>
Username: <input type="text" id="create_username"><br><br>
Welcome text: <textarea id="create_welcome" style="vertical-align: text-top;"></textarea>
<br><br>
<input type="button" value="Create an account!" id="create_account">
와의
내xml_database.xml
:
<?xml version="1.0" encoding="utf-8"?>
<user username="chris97ong" welcomeText="htrftfd"></user>
을 그래서 "chris97ong"사용자 이름 텍스트 입력에있는 경우, login.html
에 로그인 버튼을 클릭하면, I "htrftfd"라고 경고하는 것을 원합니다. 이것은 내 login.html
에 대한 자바 스크립트입니다 :
document.getElementById("sign_in").onclick = function() {
xmlDoc = loadXMLDoc("xml_database.xml");
users = xmlDoc.getElementsByTagName("user");
var username = document.getElementById("login_username").value;
var bool = false;
for(var i = 0; i < users.length && bool == false; i++) {
if(users[i].getAttribute("username") == username) {
bool = true;
alert("Your welcome text is " + users[i].getAttribute("welcomeText"));
}
}
if(bool == false) { alert("Such username does not exist"); }
}
그리고 완벽하게 작동합니다.
이제 두 번째 섹션으로 계정을 만들 수 있기를 원합니다. 계정을 만들 때 버튼을 클릭하면 xml_database.xml
에 "< 사용자 >"이라는 노드를 만들려고합니다. 나는이 새로운 요소의 "username"속성을 텍스트 입력 (id "create_username") 및이 새로운 요소의 "welcomeText"가 텍스트 영역 (id "create_welcome")과 일치하도록하고 싶습니다.
document.getElementById("create_account").onclick = function() {
xmlDoc = loadXMLDoc("xml_database.xml");
users = xmlDoc.getElementsByTagName("user");
var username = document.getElementById("create_username").value;
var bool = false;
for(var i = 0; i < users.length && bool == false; i++) {
if(users[i].getAttribute("username") == username) {
bool = true;
alert("Such username already exists");
}
}
if(bool == false) {
var welcomeText = document.getElementById("create_welcome").value;
new_user = xmlDoc.createElement("user");
new_user.setAttribute("username",username);
new_user.setAttribute("welcomeText",welcomeText);
alert("Account created");
}
}
그러나 그것은 작동하지 않습니다
이 내가 시도하는 것이다. 이 새 사용자 이름으로 로그인하려고하면 해당 사용자 이름이 존재하지 않는다는 경고 메시지가 나타납니다. 오류 메시지가 전혀없고 XML 파일이 전혀 변경되지 않았습니다. 내가 알지 못하는 코드에 어떤 문제가 있습니까? 고마워.
PS : 내 <head>
태그이 있습니다
<script src="http://www.w3schools.com/dom/loadxmldoc.js"></script>