我很感兴趣是否可以更改占位符的默认验证消息。
必须是一种制作自定义信息的方式。
我的表单:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Placeholder</title>
</head>
<body>
<form>
<input type="text" id="email" maxlength="35" placeholder="E-mail" required>
<button type="submit">Ok</button>
</form>
</body>
</html>
我尝试使用JavaScript ,但每次都会显示以下消息:
<script type="text/javascript">
document.getElementById('email').setCustomValidity('Please enter your e-mail!');
</script>
答案 0 :(得分:0)
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script>
</head>
<body>
<form>
<input type="text" id="email" maxlength="35" placeholder="E-mail" required>
<button type="submit">Ok</button>
</form>
<script>
$(document).ready(function(){
var elements = document.getElementsByTagName("INPUT");
for (var i = 0; i < elements.length; i++){
elements[i].oninvalid = function(e){
e.target.setCustomValidity("");
if (!e.target.validity.valid){
e.target.setCustomValidity("This field cannot be left blank");
}
};
elements[i].oninput = function(e){
e.target.setCustomValidity("");
};
}
})
</script>
</body>
</html>