在谷歌Chrome一些客户无法继续到我的支付页面。 当我试图提交一个表单时,我得到这个错误:

name= "无效的窗体控件不可聚焦。

这来自JavaScript控制台。

我读到这个问题可能是由于隐藏字段具有必需的属性。 现在的问题是,我们使用的是。net webforms required字段验证器,而不是html5 required属性。

谁得到这个错误似乎是随机的。 有谁知道解决办法吗?


当前回答

确保表单中具有所需属性的所有控件也具有name属性集

其他回答

对于Select2 Jquery问题

这个问题是由于HTML5验证不能聚焦隐藏的无效元素。 我在处理jQuery Select2插件时遇到了这个问题。

解决方案 你可以在表单的每个元素上注入事件监听器和“无效”事件,这样你就可以在HTML5验证事件之前进行操作。

$('form select').each(function(i){
this.addEventListener('invalid', function(e){            
        var _s2Id = 's2id_'+e.target.id; //s2 autosuggest html ul li element id
        var _posS2 = $('#'+_s2Id).position();
        //get the current position of respective select2
        $('#'+_s2Id+' ul').addClass('_invalid'); //add this class with border:1px solid red;
        //this will reposition the hidden select2 just behind the actual select2 autosuggest field with z-index = -1
        $('#'+e.target.id).attr('style','display:block !important;position:absolute;z-index:-1;top:'+(_posS2.top-$('#'+_s2Id).outerHeight()-24)+'px;left:'+(_posS2.left-($('#'+_s2Id).width()/2))+'px;');
        /*
        //Adjust the left and top position accordingly 
        */
        //remove invalid class after 3 seconds
        setTimeout(function(){
            $('#'+_s2Id+' ul').removeClass('_invalid');
        },3000);            
        return true;
}, false);          
});

我来说明一下我的情况,因为它不同于上面所有的解。我有一个html标签没有正确关闭。元素不是必需的,但它嵌入在一个隐藏的div中

在我的案例中,问题在于type="datetime-local",出于某种原因,它在表单提交时被验证。

我改变了这个

<input type="datetime-local" />

<input type="text" />

如果您有任何具有required属性的字段,但在表单提交期间不可见,则将抛出此错误。当您试图隐藏该字段时,只需删除所需的属性。如果您想再次显示该字段,您可以添加所需的属性。通过这种方式,您的验证将不会受到影响,同时,错误将不会抛出。

是的. .如果隐藏表单控件有必填字段,则会显示此错误。一种解决方案是禁用此表单控件。这是因为通常如果你隐藏一个表单控件,那是因为你不关心它的值。所以这个表单控件的名称值对在提交表单时不会被发送。

在你的表单中,你可能有隐藏的输入需要的属性:

<input type="hidden" required /> <input type="file" required style="display: none;"/>

表单不能专注于这些元素,你必须从所有隐藏的输入中删除required,或者在javascript中实现一个验证函数来处理它们,如果你真的需要一个隐藏的输入。