2014-10-30 123 views
1

我想从我的页面中删除一个div,一旦收到XMPHttpRequest响应。我也尝试将div的显示设置为none:document.getElementById('password')。style.display ='none';但那也没用。使用JavaScript从页面中删除div

HTML:

<div class="login-container"> 
     <div class="title">Login Form</div> 
     <div class="form-fields" id="form-fields"> 
      <form name="login_form" method="post" id="login-form" onsubmit="return submitForm()"> 
       <input type="text" name="username" id="username" placeholder="Username" required></input> 
       <input type="password" name="password" id="password" placeholder="Password" required></input> 
       <input type="submit" value="Log in" id="submit-button"></input> 
       <input id="sessionID" class="hidden"></input> 
      </form> 
     </div> 
     <div id="success-message" class="hidden">Congratulations! You have been logged in.</div> 
     <button id="logout" class="hidden"></button> 
</div> 

JavaScript函数:

function sendLoginInfo() { 
     var loginRequest = new XMLHttpRequest(); 
     loginRequest.open("POST", "server/login.php", false); 
     loginRequest.setRequestHeader('Content-type', "application/json"); 
     loginRequest.onreadystatechange = function() { 
      if(loginRequest.readyState == 4 && loginRequest.status == 200) { 
       sessionID = keyRequest.responseText; 
       var el = document.getElementById('password'); 
       el.parentNode.removeChild(el); 

      } 
     } 
     loginRequest.send(JSON.stringify(JSONFormData)); 
} 

更新

这个问题已经解决了改变:

<form name="login_form" method="post" id="login-form" onsubmit="return submitForm()"> 

收件人:

<form name="login_form" method="post" id="login-form" onsubmit="submitForm(); return false;"> 
+0

也看到这个:http://stackoverflow.com/questions/3387427/javascript-remove-element-by-id – 2014-10-30 22:35:06

+2

你的'if'实际上是否进入? – 2014-10-30 22:44:13

+0

编写的代码应该可以工作,如果执行**'var el = document.getElementById('password')'并返回一个元素。 – RobG 2014-10-30 22:46:20

回答

-1

代替

document.getElementById('password').style.display = 0; 

尝试

document.getElementById('password').style.display = 'none'; 
+0

对不起,这是一个错字,那是我试过的 – Micah 2014-10-30 22:36:58

-1

你真的无法删除的div据我知道,但你可以设置一个div的可见性“没有“(不是你试过的”0“)。

style="display:none" 

这使得div无形并且不会有任何空白。

+0

对不起是一个错字,这就是我试过 – Micah 2014-10-30 22:37:33

+0

的CSS规则都设置在不同的文件。由于某种原因,当我尝试设置它不在元素上设置的样式时 – Micah 2014-10-30 22:38:11