HTML 5表单必需的属性。设置自定义验证消息?我有以下HTML 5表单:http://jsfiddle.net/nfgfP/<form id="form" onsubmit="return(login())"><input name="username" placeholder="Username" required /><input name="pass" type="password" placeholder="Password" required/><br/>Remember me: <input type="checkbox" name="remember" value="true" /><br/><input type="submit" name="submit" value="Log In"/>当前,当我点击Enter时,它们都是空的,弹出框将显示“请填写此字段”。如何将默认消息更改为“此字段不能留空”?编辑:还请注意,类型密码字段的错误消息很简单*****..要重新创建它,给用户名一个值,然后点击Submit。编辑:我用Chrome 10进行测试。请做同样的事
3 回答
缥缈止盈
TA贡献2041条经验 获得超4个赞
document.addEventListener("DOMContentLoaded", 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(""); }; }})
I
编辑
setCustomValidity
setCustomValidity
进一步编辑
invalid
oninvalid
SMILET
TA贡献1796条经验 获得超4个赞
<input type="text" id="username" required placeholder="Enter Name" oninvalid="this.setCustomValidity('Enter User Name Here')" oninput="this.setCustomValidity('')" />
oninput="this.setCustomValidity('')"
ITMISS
TA贡献1871条经验 获得超8个赞
HTML5
oninvalid
<input id="UserID" type="text" required="required" oninvalid="this.setCustomValidity('Witinnovation')" onvalid="this.setCustomValidity('')">
onvalid="this.setCustomValidity('')"
添加回答
举报
0/150
提交
取消