[英]Is there a way to get rid of the error message I created in javascript after the user does the right thing?
The code will push a message saying 'First Name Can't be empty', that is when a user clicks on a button before typing their First name.该代码将推送一条消息,说明“名字不能为空”,即用户在输入名字之前单击按钮时。 This is great, however, after they get the message, if they do type their first name and submit, the pushed message does not go away!
这很好,但是,在他们收到消息后,如果他们确实输入了他们的名字并提交,推送的消息不会消失!
Is there a way for the pushed message ('First Name Can't be empty') to go away once the user has typed their first name?一旦用户输入了他们的名字,有没有办法让推送的消息(“名字不能为空”)消失?
Many thanks.非常感谢。
const name = document.getElementById('FirstName') const lastName = document.getElementById('LastName') const email = document.getElementById('EmailAddress') const password = document.getElementById('Password') const form = document.getElementById('form') const errorElement1 = document.getElementById('error-FirstName') const errorElement2 = document.getElementById('error-LastName') const errorElement3 = document.getElementById('error-Email') const errorElement4 = document.getElementById('error-Password') form.addEventListener('submit', (e) => { let messages = [] if (name.value === '' || name.value === null) { messages.push('First Name Can\\'t be empty') } if (messages.length > 0) { e.preventDefault() errorElement1.innerText = messages.join(', ') } })
Clear the error element when there are no errors.当没有错误时清除错误元素。
const name = document.getElementById('FirstName') const lastName = document.getElementById('LastName') const email = document.getElementById('EmailAddress') const password = document.getElementById('Password') const form = document.getElementById('form') const errorElement1 = document.getElementById('error-FirstName') const errorElement2 = document.getElementById('error-LastName') const errorElement3 = document.getElementById('error-Email') const errorElement4 = document.getElementById('error-Password') form.addEventListener('submit', (e) => { let messages = [] if (name.value === '' || name.value === null) { messages.push('First Name Can\\'t be empty') } if (messages.length > 0) { e.preventDefault() errorElement1.innerText = messages.join(', ') } else { errorElement1.innerText = ''; } })
Yes, you can just use the innerHTML property of the errorElement after you use a condition to check if the submit has been done correctly是的,您可以在使用条件检查提交是否正确完成后使用 errorElement 的 innerHTML 属性
const name = document.getElementById('FirstName') const lastName = document.getElementById('LastName') const email = document.getElementById('EmailAddress') const password = document.getElementById('Password') const form = document.getElementById('form') const errorElement1 = document.getElementById('error-FirstName') const errorElement2 = document.getElementById('error-LastName') const errorElement3 = document.getElementById('error-Email') const errorElement4 = document.getElementById('error-Password') form.addEventListener('submit', (e) => { let messages = [] if (name.value === '' || name.value === null) { messages.push('First Name Can\\'t be empty') } else { //clear messages messages = []; errorLement1.innerHTML=""; } if (messages.length > 0) { e.preventDefault() errorElement1.innerText = messages.join(', ') } })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.