看这个演示:http: //jsfiddle.net/3JeZ6/10/
这是它的 HTML:
<style>
.valid { color: #0d0; }
.invalid { color: #d00; }
</style>
<form id="testform" action="/">
<label>Required:
<input id="required_input" required="">
</label><br>
<label>Pattern ([0-9][A-Z]{3}):
<input id="pattern_input" pattern="[0-9][A-Z]{3}">
</label><br>
<label>Min (4):
<input id="min_input" type="number" min="4">
</label><br>
<input type="submit" value="Submit Button" id="sbmt" />
</form>
<input type="button" value="Trigger click on submit button" id="test">
<input type="button" value="Trigger form submit" id="test1">
<input type="button" value="Run checkValidity" id="test2">
对我来说这很疯狂,但简单form.submit()
真的行不通。但是,如果您触发点击提交按钮 - 一切正常:
$("#test").click(function(){
$("#sbmt").click(); // works fine
})
$("#test1").click(function(){
$("#testform").submit();// does not work
})
$("#test2").click(function(){
alert($("#testform")[0].checkValidity() ? "valid": "not valid"); // result looks to be correct, but it does not highlight fields that are not valid
})
我在最新的 Chrome 和 FireFox 中确认了以下行为