javascript 如何在javascript中的文本框旁边显示错误消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18954388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to display error message next to textbox in javascript?
提问by Ashok kumar
I am new to Javascript. So I had a doubt for the following function, how to display the error message next to text box? right now it is showing a alert message, but I need to change the alert message and need to display it next to textbox?
我是 Javascript 的新手。所以我对下面的功能有疑问,如何在文本框旁边显示错误信息?现在它显示一条警报消息,但我需要更改警报消息并需要在文本框旁边显示它?
function AllowLock(){
if (!locker.lock.value.match(/[a-zA-Z]$/) && locker.lock.value !="") {
locker.lock.value="";
alert("Please Enter only valid lock");
}
if(locker.lock.value.length > 5)
alert("max length exceeded");
}
}
采纳答案by Anna Gabrielyan
you must have any container for that message, so you can create element and then append your message to that, something like this, with jquery
您必须有该消息的任何容器,因此您可以创建元素,然后使用 jquery 将消息附加到该元素,就像这样
<input type="text" id="kuku"></input>
$('#kuku').after('<div></div>').html("your message");
回答by andorx
You can create a span element next to your textbox
您可以在文本框旁边创建一个 span 元素
<input type="text" /><span id="msg"></span>
and put your message.
并把你的消息。
document.getElementById("msg").innerHTML = "your message";