html5表单验证用placeholder显示错误提示 5
js效果:
$('.register form').submit(function(){
var username = $(this).find('input#username').val();
if(username == '') {
document.getElementById("username").setAttribute("placeholder","请输入您的姓名/团队名称");
return false;
}
});
代码:
<div class="register">
<form action="" method="post">
<ul class="form-list">
<li>
<label for="username"> </label>
<input type="text" id="username" name="username" placeholder="用户名" >
</li>
<li>
<label for="password"> </label>
<input type="password" id="password" name="password" placeholder="密码">
</li>
<li>
<button type="submit">登陆</button>
</li>
</ul>
</form>
</div> 展开
2016-06-20 · 百度知道合伙人官方认证企业
html5表单验证用placeholder显示错误提示:
1、html5包含校验的placeholder如下:
<form action="#" method="post">
<div>
<label for="name">Text Input:</label>
<input type="text" placeholder="Your name" name="name" id="name" value="" tabindex="1" />
</div>
<div>
<label for="name_2">Text Input 2:</label>
<input type="text" placeholder="Your name" name="name_2" id="name_2" value="" tabindex="1" />
</div>
<div>
<label for="textarea">Textarea:</label>
<textarea placeholder="Some text" cols="40" rows="8" name="textarea" id="textarea"></textarea>
</div>
<div>
<label for="textarea">Textarea 2:</label>
<textarea placeholder="Some text" cols="40" rows="8" name="textarea_2" id="textarea_2"></textarea>
</div>
<div>
<input type="submit" value="Submit" />
</div>
</form>
2、css样式:
::-webkit-input-placeholder {
color: #999;
}
:-moz-placeholder {
color: #999;
}
::-moz-placeholder {
color: #999;
}
:-ms-input-placeholder {
color: #999;
}
/* Different color for some fields */
#name_2::-webkit-input-placeholder,
#textarea_2::-webkit-input-placeholder
{
color: #FF0000;
}
#name_2:-moz-placeholder,
#textarea_2:-moz-placeholder
{
color: #FF0000;
}
#name_2::-moz-placeholder,
#textarea_2::-moz-placeholder
{
color: #FF0000;
}
#name_2:-ms-input-placeholder,
#textarea_2:-ms-input-placeholder
{
color: #FF0000;
}
3、运行效果:
提交出错就是红色的