MDL jQuery验证集成:mdl-radio__button验证所需的工作

前端之家收集整理的这篇文章主要介绍了MDL jQuery验证集成:mdl-radio__button验证所需的工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
与MDL相似的是jQuery.validate的正常使用情况已经失败了.见 this gist.

这工作正常

  1. <h1>Without MDL</h1>
  2. <form id="foo">
  3. <input type="radio" name="bar" value="1" /> 1
  4. <input type="radio" name="bar" value="2" /> 2
  5. <input type="submit" />
  6. </form>
  7. <script>
  8. $(function() {
  9. $('#foo').validate({
  10. rules: {
  11. "bar": {
  12. required: true
  13. },},errorPlacement: function(error,element) {
  14. console.log(element);
  15. }
  16. });
  17. });
  18. </script>

这什么也不做:

  1. <h1>With MDL</h1>
  2. <form id="zha">
  3. <label for="baz__1" class="mdl-radio mdl-js-radio">
  4. <input type="radio" name="baz" value="1" id="baz__1" class="mdl-radio__button" /> 1
  5. </label>
  6. <label for="baz__2" class="mdl-radio mdl-js-radio">
  7. <input type="radio" name="baz" value="2" id="baz__2" class="mdl-radio__button" /> 2
  8. </label>
  9. <input type="submit" id="butt"/>
  10. </form>
  11. <script>
  12. $(function() {
  13. $('#zha').validate({
  14. rules: {
  15. "baz": {
  16. required: true
  17. },element) {
  18. console.log(element);
  19. }
  20. });
  21. });
  22. </script>

在MDL版本中,附加jQuery.validator.setDefaults({debug:true})在零调试输出中没有任何作用.删除mdl-js-radio或mdl-radio__button使其按预期工作.我的直觉是,MDL正在以一种断开jQuery对name = attributes的访问方式来更新DOM,但是我找不到任何支持MDL源代码的证据.

有人在这里工作吗?

解决方法

经过一番研究,我想我找到了一个解决你的问题的办法.这是一个有趣的.
对于我来说,您的代码适用于Firefox,但不适用于Chrome和Safari.原因很简单. MDL删除您的输入单选按钮的CSS默认样式以隐藏它们(不显示:none,只有height:0,width:0,opacity …). Chrome和Safari认为像“隐藏”一样.浏览jQuery.validate代码,我意识到输入单选按钮从未被发现. (您的代码适用于Chrome和Safari Safari上的经典输入).为什么?因为如果你看看jQuery.validate的默认设置,你可以看到他忽略了隐藏的输入.
  1. defaults: {
  2. ...
  3. ignore: ":hidden",...
  4. onfocusin: function( element,event ) {

和滤镜功能

  1. elements: function() {
  2. var validator = this,rulesCache = {};
  3.  
  4. // select all valid inputs inside the form (no submit or reset buttons)
  5. return $(this.currentForm)
  6. .find("input,select,textarea")
  7. .not(":submit,:reset,:image,[disabled]")
  8. .not( this.settings.ignore ) // This line
  9. .filter(function() {
  10. if ( !this.name && validator.settings.debug && window.console ) {
  11. console.error( "%o has no name assigned",this);
  12. }
  13.  
  14. // select only the first element for each name,and only those with rules specified
  15. if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) {
  16. return false;
  17. }
  18.  
  19. rulesCache[this.name] = true;
  20. return true;
  21. });
  22. },

为了防止这个只是添加这段代码

  1. jQuery.validator.setDefaults({
  2. ignore: ""
  3. });

它适用于我希望对你有效.

猜你在找的jQuery相关文章