如何通过 JavaScript 显示错误消息?
我正在创建一个表单,并使用 JavaScript 对其进行验证。它现在的工作方式是,如果值不正确,它会显示一条警报,指出您需要输入文本。我的问题是,如何更改它,以便文本框变为红色而不是警报,并且文本框旁边将显示类似:“这是必需的”的内容。我认为我可以用 jQuery 解决这个问题,但我想知道是否有人有任何建议。谢谢!
PS 如果您访问 yahoo.com,请单击“注册”,然后单击创建帐户(无需输入任何内容),您就会看到我的目标。
I'm creating a form, and I'm validating it with JavaScript. The way it works now, is if a value is not correct, it displays an alert stating that you need to enter text. My question is, how can I change it so that instead of an alert, the text box will turn red, and something like: "This is required" will show next to the textbox. I think I can solve this with jQuery, but I was wondering if anyone had any suggestions. THANKS!
P.S. If you go to yahoo.com, click Sign Up, and click Create Account (without entering anything), you'll see what I'm aiming at.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个具有以下属性的 CSS 类:
使用 jQuery,您可以将此类添加到具有不正确值的输入框:(
假设名称为“Address”的输入框具有错误值)
You could create a CSS class with the following properties:
Using jQuery, you can add this class to the input box with the incorrect value:
(assuming the input box with the name "Address" had the wrong value)
好吧,我会使用一个 jQuery 验证插件,它位于
http://bassistance.de/ jquery-plugins/jquery-plugin-validation/
你只需要将css类“required”添加到需要验证的元素中
Well , I would use a jQuery Validation plugin which is at
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
You just need to add the css class "required" to the elements that you need to be validated