0

如果可以更改占位符的默认验证消息,我很感兴趣。
必须是一种制作自定义消息的方法。

我的表格:

<!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>
4

1 回答 1

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>
于 2013-07-03T08:19:02.597 回答