简体   繁体   English

防止Firefox在表单首次加载时突出显示“无效”(html5)复选框

[英]Prevent firefox from highlighting “invalid” (html5) checkboxes at form first load

I'm using html5 validations for a form, using webshim (a polyfill solution). 我正在使用webshimpolyfill解决方案)对表单使用html5验证。 Webshim made a validation for group checkboxes- that you must check at least one of the group: ( here , search for data-grouprequired ) Webshim对组复选框进行了验证-您必须检查组中的至少一个:( 在这里 ,搜索data-grouprequired

I made an improved validation that also includes a minimum and a maximum boxes that must be checked. 我做了改进的验证,其中还包括必须选中的最小和最大框。

Both work (here's a working fiddle with both) BUT in firefox (not chrome nor ie11) after page first loads, the "invalid" checkboxes are already highlighted in red, as seen in given fiddle- only with firefox. 这两个工作(这里的工作小提琴与两个),但在Firefox(不镀铬也不IE11)第一次加载页面之后,“无效”复选框已经以红色突出显示,如只用Firefox给出fiddle-看到。

So my question is: How to I prevent Firefox from highlighting those "invalid" as the page loads? 所以我的问题是:如何防止Firefox在页面加载时突出显示那些“无效”的内容? (User didn't even have a chance to fill those fields yet) (用户甚至没有机会填写这些字段)

I created this fiddle so you can easily try things: fiddle 我创造了这个小提琴让您可以轻松尝试的事情: 小提琴

Thank you. 谢谢。

  • I added the Webshim example to show that the "problem" (different behavior) is not so much with my code as it is with firefox (picture). 我添加了Webshim示例,以表明代码中的“问题”(不同的行为)与firefox(图片)的关系不大。

在此处输入图片说明

Here is the code: html: 这是代码:html:

<form action="#">
    <div class="form-row">
        <label>checkboxes with webshim groupRequired:</label>
        <div id="foo5">
            <input name="b" type="checkbox" data-grouprequired="" />
            <input name="b" type="checkbox" />
            <input name="b" type="checkbox" />
            <input name="b" type="checkbox" />
        </div>
    </div>
    <div class="form-row">
        <label>checkboxes with my custom min/max (min:2, max:3) :</label>
        <div id="foo">
            <input id="cb1" class="cb" name="a" data-max="3" data-min="2" type="checkbox" />
            <input id="cb2" class="cb" name="a" data-max="3" data-min="2" type="checkbox" />
            <input id="cb3" class="cb" name="a" data-max="3" data-min="2" type="checkbox" />
            <input id="cb4" class="cb" name="a" data-max="3" data-min="2" type="checkbox" />
        </div>
    </div>
    <div class="form-row">
        <label for="name">Name</label>
        <input class="foo3" type="text" id="name" required="" />
    </div>
    <div class="form-row">
        <input type="submit" />
    </div>
</form>

jquery: jQuery的:

 //webshim.setOptions
 webshim.setOptions("forms", {
     lazyCustomMessages: true,
     replaceValidationUI: true,
     customDatalist: "auto",
     addValidators: true

 });
 //request the features you need:
 webshim.polyfill('forms');

 $(function () {
     // use all implemented API-features on DOM-ready
     //webshim.activeLang(); //returns current set language

     webshim.activeLang('en'); //set locale to en

     i3 = 0;
     i4 = 0;
     first_arr = {};
     $('.cb').each(function (index, wrap) {
         first_arr[$(this).attr('id')] = ++i3;
         $(this).on('validatevalue', function (e, extra) {
             //failed fix attempt:
             if (false && first_arr[$(this).attr('id')] > 0) {
                 first_arr[$(this).attr('id')] = --i4;
                 return;
             }//end of failed fix attempt
             var elem = e.currentTarget;
             var min = $(e.currentTarget).data('min');
             var max = $(e.currentTarget).data('max');
             var total = $(elem).parents('#foo').find('input:checked').length;

             if (min && min > 0 && max && max > 0 && (total > max || total < min)) {
                 return 'Must select between ' + min + ' to ' + max;
             }
             if (min && min > 0 && total < min) {
                 return "can't select less than " + min;
             }
             if (max && max > 0 && total > max) {
                 return "can't select more than " + max;
             }
             $('.cb').not($(elem)).each(function () {
                 $(this).setCustomValidity("");
             });
         });
     });
 });

I'll also add how Webshim did their own custom, checkbox grouprequired validation, their github file : 我还将添加Webshim如何进行自己的自定义,复选框组所需的验证以及其github文件

addCustomValidityRule('grouprequired', function(elem, val, data){
        var form, name;
        if(!('grouprequired' in data) || elem.type !== 'checkbox' || !(name = elem.name)){return;}

        if(!data.grouprequired.checkboxes){
            data.grouprequired = {};
            data.grouprequired.checkboxes = $( ((form = $.prop(elem, 'form')) && form[name]) || document.getElementsByName(name)).filter('[type="checkbox"]');
            data.grouprequired.checkboxes
                .off('click.groupRequired')
                .on('click.groupRequired', function(){
                    if((data.customMismatchedRule == 'grouprequired') == this.checked){
                        $(elem).trigger('updatevalidation.webshims');
                    }
                })
            ;

            data.grouprequired.checkboxes.not(elem).removeData('grouprequired');
        }

        return !(data.grouprequired.checkboxes.filter(':checked:enabled')[0]);
    }, 'Please check one of these checkboxes.');

Try: 尝试:

input[type='checkbox']:invalid { box-shadow: none !important; }

See: https://hacks.mozilla.org/2010/11/firefox-4-html5-forms/ 请参阅: https//hacks.mozilla.org/2010/11/firefox-4-html5-forms/

Do a find on the page for ":invalid" 在页面上查找“:invalid”

You may also need: 您可能还需要:

input[type='checkbox']:required { box-shadow: none !important; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM