Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 防止firefox突出显示;“无效”;(html5)表单首次加载时的复选框_Jquery_Html_Firefox_Webshim - Fatal编程技术网

Jquery 防止firefox突出显示;“无效”;(html5)表单首次加载时的复选框

Jquery 防止firefox突出显示;“无效”;(html5)表单首次加载时的复选框,jquery,html,firefox,webshim,Jquery,Html,Firefox,Webshim,我正在使用html5验证表单,使用(一个polyfill解决方案)。 Webshim对组复选框进行了验证-您必须至少选中组中的一个:(,搜索数据组所需的) 我做了一个改进的验证,其中还包括必须选中的最小值和最大值框 这两个选项都可以使用(这里是一个同时使用两个选项的选项),但是在firefox(不是chrome或ie11)中,在页面首次加载后,“无效”复选框已经以红色突出显示,如给定的fiddle中所示-仅适用于firefox 所以我的问题是:如何防止Firefox在页面加载时突出显示那些“无效

我正在使用html5验证表单,使用(一个polyfill解决方案)。 Webshim对组复选框进行了验证-您必须至少选中组中的一个:(,搜索数据组所需的

我做了一个改进的验证,其中还包括必须选中的最小值和最大值框

这两个选项都可以使用(这里是一个同时使用两个选项的选项),但是在firefox(不是chrome或ie11)中,在页面首次加载后,“无效”复选框已经以红色突出显示,如给定的fiddle中所示-仅适用于firefox

所以我的问题是:如何防止Firefox在页面加载时突出显示那些“无效”的内容?(用户甚至还没有机会填写这些字段)

我制作了这把小提琴,这样你就可以轻松地尝试:

多谢各位

  • 我添加了Webshim示例来说明“问题”(不同的行为)与其说是我的代码,不如说是firefox(图)

代码如下: html:

尝试:

input[type='checkbox']:无效{
盒影:无!重要;
}

见:

在页面上查找“:invalid”

您可能还需要:

input[type='checkbox']:必需{
盒影:无!重要;
}

<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>
 //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("");
             });
         });
     });
 });
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.');