简体   繁体   English

document.getElementById JS HTML CSS

[英]document.getElementById JS HTML CSS

I am trying to get my forms NOT TO reset. 我正在尝试让我的表格不要重设。 I am making a <input type="password" /> and whenever I click the button to submit the password, and they get it wrong, I need there to be something that shows that it is incorrect. 我正在创建一个<input type="password" /> ,每当我单击按钮提交密码时,如果密码输入错误,我就需要显示一些错误信息。 It works, but only for a split second. 它有效,但仅在瞬间。 Can you help me? 你能帮助我吗?

 function desktop() { var pass = document.getElementById("pass").value; if (pass == "555") { alert("Welcome;") } else { document.getElementById("wrg").innerHTML = "Incorrect Password"; } } 
 #pass { border-radius: 8px; padding: 5px; margin-bottom: 5px; } #user { margin-bottom: 10px; padding-bottom: 0px; margin-top: 220px; } #wrg { visibility: visible; } 
 <form> <center> <h1 id="user">User</h1> <input type="password" id="pass" placeholder=" Password" /><br> <button onclick="desktop()" id="pass">Sign In</button> <p id="wrg"></p> </center> </form> 

The button ends up submitting the form, which causes the window to refresh because there's no action attribute on the form. 该按钮最终提交了表单,这导致窗口刷新,因为表单上没有action属性。

You can prevent this by either making the button type="button" (rather than the default submit ), using event.preventDefault() , or by returning false, as below. 您可以防止这种通过任何使按钮type="button" (而不是默认的submit ),使用event.preventDefault()下文,或返回false。 (But as noted in comments below, return false may not be the best approach: it's easy to forget to include the return in both the function and the onclick attribute, without which the form will submit anyway. event.preventDefault is the most explicit and therefore probably best way to handle this.) (但是如下面的注释所述, return false可能不是最好的方法:忘记将return包含在函数和onclick属性中很容易,否则表单将始终提交event.preventDefault是最显式的,并且因此可能是处理此问题的最佳方法。)

 function desktop() { var pass = document.getElementById("pass").value; if (pass == "555") { alert("Welcome;") } else { document.getElementById("wrg").innerHTML = "Incorrect Password"; } return false; } 
 #pass { border-radius: 8px; padding: 5px; margin-bottom: 5px; } #user { margin-bottom: 10px; padding-bottom: 0px; /*margin-top: 220px;*/ } 
 <form> <center> <h1 id="user">User</h1> <input type="password" id="pass" placeholder=" Password" /><br> <button onclick="return desktop()">Sign In</button> <p id="wrg"></p> </center> </form> 

(You do have duplicate pass IDs, which should be unique, and of course clientside authentication as done here isn't the least bit secure, but neither of those issues is directly relevant to your question. getElementById winds up returning the first matching element, which happens to be the one you wanted.) (您确实有重复的pass ID,该ID应该是唯一的,当然,此处进行的客户端身份验证并不是最不安全的方法,但是这些问题都不与您的问题直接相关getElementById最终会返回第一个匹配的元素,恰好是您想要的那个。)

You are using duplicate IDs for your button and input elements: pass . 您正在为按钮和输入元素使用重复ID: pass

Also, it would be easier to just add an event listener to the Sign In button and capture that event inside your function. 而且,将事件侦听器添加到“登录”按钮并在函数中捕获该事件会更容易。

document.getElementById("pass").addEventListener('click', desktop);

function desktop(evt) {
  evt.preventDefault();
  var pass = document.getElementById("pass").value;
  if (pass == "555") {
    alert("Welcome;");
  } else {
    document.getElementById("wrg").innerHTML = "Incorrect Password";
  }
}

If you do it this way, remember to remove the onclick attribute from the button. 如果这样做,请记住从按钮中删除onclick属性。

Are you saying that the fields within the form are resetting, or are you saying that the paragraph element with id 'wrg' is not staying? 您是说表单中的字段正在重置,还是说ID为'wrg'的段落元素没有保留?

To keep the form fields from resetting you should be passing all the form values to a PHP file so you can parse the file. 为了防止表单字段被重置,您应该将所有表单值传递给PHP文件,以便您可以解析该文件。 If you find out that the password is incorrect, then take the user back to the same form with the same values put back in, along with any other helper elements around the form. 如果您发现密码不正确,则将用户返回到具有相同值的相同表单,以及表单周围的任何其他帮助元素。

To get the element with id 'wrg' to stay you have to know how an HTML form works. 要使ID为'wrg'的元素保留下来,您必须知道HTML表单的工作方式。 Once a HTML form is sent the page is often reloaded, and since you have not specified an 'action' for the form it is being sent to the same file. 发送HTML表单后,通常会重新加载该页面,并且由于您尚未为表单指定“操作”,因此会将其发送到同一文件。 I believe you are having this trouble because the form is reloading the page. 我相信您遇到了麻烦,因为表单正在重新加载页面。

On a side note make sure you parse passwords on the backend of the server because users can disable JavaScript and bypass your password verification process. 附带说明一下,请确保您解析服务器后端的密码,因为用户可以禁用JavaScript并绕过您的密码验证过程。

I may be wrong, but I hope this helps you :) 我可能是错的,但我希望这对您有帮助:)

profile for Dalton at Stack Overflow, Q&A for professional and enthusiast programmers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM