【问题标题】:HTML5 placeholder required - Change the default message需要 HTML5 占位符 - 更改默认消息
【发布时间】:2014-03-14 10:12:11
【问题描述】:

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

我的表格:

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

【问题讨论】:

标签: html message default placeholder required


【解决方案1】:
<!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>

【讨论】:

    猜你喜欢
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多