简体   繁体   English

在用户做了正确的事情后,有没有办法摆脱我在 javascript 中创建的错误消息?

[英]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.

相关问题 如何摆脱“请求的不安全字体”错误消息? - How do I get rid of `requested insecure font` error message? 如何摆脱 javascript 中的控制台错误 - How can I get rid of a console error in javascript 我可以在创建后立即获取文档密钥吗? - Can I get the document key right after it is created? 如何从 Firebase 中删除此错误消息? - How to get rid of this error message from Firebase? 我如何摆脱 message.delete(); dms 中的错误消息? - How do I get rid of message.delete(); error message in dms? Javascript 函数不会在我创建的函数之后运行 - Javascript function does not run after a function that I created 过滤掉除空白数组条目没有错误,但不起作用(javascript) - filter to get rid of blank array entries gives no error, but does not work (javascript) 当用户单击关闭窗口时创建的弹出消息时,是否可以从用户那里获得响应? - Is there a way to get the response from a user when they click on the pop up message created when they close the window? 我无法摆脱最后一次调用map bind时创建的标记 - I can not get rid of the markers that was created in the last call to map bind 在 JavaScript 中使用 function 转换温度时,如何消除 NaN 错误? - How do I get rid of the NaN error when converting temperature with a function in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM