使用MDL的jQuery.validate的正常用例似乎已经出错了。请参阅this gist。
这很好用:
<h1>Without MDL</h1>
<form id="foo">
<input type="radio" name="bar" value="1" /> 1
<input type="radio" name="bar" value="2" /> 2
<input type="submit" />
</form>
<script>
$(function() {
$('#foo').validate({
rules: {
"bar": {
required: true
},
},
errorPlacement: function(error, element) {
console.log(element);
}
});
});
</script>
这 nothing :
<h1>With MDL</h1>
<form id="zha">
<label for="baz__1" class="mdl-radio mdl-js-radio">
<input type="radio" name="baz" value="1" id="baz__1" class="mdl-radio__button" /> 1
</label>
<label for="baz__2" class="mdl-radio mdl-js-radio">
<input type="radio" name="baz" value="2" id="baz__2" class="mdl-radio__button" /> 2
</label>
<input type="submit" id="butt"/>
</form>
<script>
$(function() {
$('#zha').validate({
rules: {
"baz": {
required: true
},
},
errorPlacement: function(error, element) {
console.log(element);
}
});
});
</script>
在MDL版本中附加jQuery.validator.setDefaults({ debug: true })
对 zero 调试输出没有影响。删除mdl-js-radio
或mdl-radio__button
可使其按预期工作。我的直觉是MDL以断开jQuery对name=
属性的访问权限的方式更新DOM,但我找不到任何证据支持MDL源代码。
有人在这里有合作吗?
答案 0 :(得分:5)
defaults: {
...
ignore: ":hidden",
...
onfocusin: function( element, event ) {
过滤功能
elements: function() {
var validator = this,
rulesCache = {};
// select all valid inputs inside the form (no submit or reset buttons)
return $(this.currentForm)
.find("input, select, textarea")
.not(":submit, :reset, :image, [disabled]")
.not( this.settings.ignore ) // This line
.filter(function() {
if ( !this.name && validator.settings.debug && window.console ) {
console.error( "%o has no name assigned", this);
}
// select only the first element for each name, and only those with rules specified
if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) {
return false;
}
rulesCache[this.name] = true;
return true;
});
},
为了防止这种情况,只需添加以下代码:
jQuery.validator.setDefaults({
ignore: ""
});
它对我有用。希望它适合你。