名称=''的无效表单控件不可聚焦

本文翻译自:An invalid form control with name='' is not focusable

I have an acute problem on my website. 我的网站上有一个严重的问题。 In Google Chrome some customers are not able to proceed to my payment page. 在Google Chrome浏览器中,某些客户无法进入我的付款页面。 When trying to submit a form I get this error: 尝试提交表单时出现此错误:

An invalid form control with name='' is not focusable.

This is from the JavaScript console. 这来自JavaScript控制台。

I read that the problem could be due to hidden fields having the required attribute. 我读到问题可能是由于隐藏字段具有必填属性。 Now the problem is that we are using .net webforms required field validators, and not the html5 required attribute. 现在的问题是,我们使用的是.net Webforms必需的字段验证器,而不是html5必需的属性。

It seems random who gets this error. 似乎谁得到此错误是随机的。 Is there anyone who knows a solution for this? 有谁知道解决方案吗?


#1楼

参考:https://stackoom.com/question/1Uvj6/名称-的无效表单控件不可聚焦


#2楼

向表单添加novalidate属性将有助于:

<form name="myform" novalidate>

#3楼

In case anyone else has this issue, I experienced the same thing. 万一其他人遇到这个问题,我也会遇到同样的事情。 As discussed in the comments, it was due to the browser attempting to validate hidden fields. 如评论中所讨论的,这是由于浏览器试图验证隐藏字段而引起的。 It was finding empty fields in the form and trying to focus on them, but because they were set to display:none; 它正在查找表单中的空字段并试图将其重点关注,但是因为它们被设置为display:none; , it couldn't. ,它不能。 Hence the error. 因此,错误。

I was able to solve it by using something similar to this: 我可以通过使用类似于以下内容的方法来解决此问题:

$("body").on("submit", ".myForm", function(evt) {

    // Disable things that we don't want to validate.
    $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", true);

    // If HTML5 Validation is available let it run. Otherwise prevent default.
    if (this.el.checkValidity && !this.el.checkValidity()) {
        // Re-enable things that we previously disabled.
        $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", false);
        return true;
    }
    evt.preventDefault();

    // Re-enable things that we previously disabled.
    $(["input:hidden, textarea:hidden, select:hidden"]).attr("disabled", false);

    // Whatever other form processing stuff goes here.
});

Also, this is possibly a duplicate of "Invalid form control" only in Google Chrome 此外,这可能仅在Google Chrome中“无效的表单控件”的重复项


#4楼

In your form , You might have hidden input having required attribute: 在您的form ,您可能隐藏了具有required属性的input

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

The form can't focus on those elements, you have to remove required from all hidden inputs, or implement a validation function in javascript to handle them if you really require a hidden input. form不能专注于这些元素,如果您确实需要隐藏的输入,则required从所有隐藏的输入中删除required元素,或者在javascript中实现验证功能以处理它们。


#5楼

It will show that message if you have code like this: 如果您具有以下代码,它将显示该消息:

<form>
  <div style="display: none;">
    <input name="test" type="text" required/>
  </div>

  <input type="submit"/>
</form>

#6楼

You may try .removeAttribute("required") for those elements which are hidden at the time of window load. 您可以尝试.removeAttribute("required")那些在窗口加载时隐藏的元素。 as it is quite probable that the element in question is marked hidden due to javascript (tabbed forms) 因为所讨论的元素很可能由于javascript(选项卡式表单)被标记为隐藏

eg 例如

if(document.getElementById('hidden_field_choice_selector_parent_element'.value==true){
    document.getElementById('hidden_field').removeAttribute("required");        
}

This should do the task. 这应该完成任务。

It worked for me... cheers 对我有用...干杯

<think>好的,用户现在想知道如何判断当前焦点控件是否是textarea。之前他们问了如何模拟按键输入到textarea,现在可能是在开发一个需要处理多个控件焦点的界面,比如带有多个输入框的表单,或者自定义的软键盘。他们可能需要在用户点击某个按钮或执行某些操作时,确认当前焦点是否在textarea上,以便正确地处理输入或执行其他操作。 首先,我需要回忆LVGL中焦点管理的机制。LVGL使用组(group)来管理焦点,控件必须被添加到组中才能接收焦点。每个输入设备(比如键盘或编码器)可以关联到一个组,组内的控件可以通过按键或编码器导航。 用户的问题是如何判断当前焦点控件是否是textarea。可能的解决方法是获取当前焦点所在的控件,然后检查其类型是否为textarea。在LVGL中,可以使用lv_group_get_focused()函数来获取当前组内获得焦点的对象,然后使用lv_obj_check_type()来验证对象类型。 但需要注意,一个组可能有多个控件,而输入设备可能关联到不同的组。因此,可能需要遍历所有活动的组,或者确保目标textarea所在的组是当前活动的组。此外,用户需要确保textarea已经被添加到某个组中,并且启用了焦点标志,比如使用lv_obj_add_flag(obj, LV_OBJ_FLAG_CLICK_FOCUSABLE)来允许通过点击获取焦点。 还有可能用户没有正确设置组,或者焦点没有按预期切换,导致判断失败。因此,在代码示例中需要包括如何创建组、添加控件到组中,以及如何检查当前焦点对象的类型。 另外,用户可能希望有一个直接的函数调用来完成这个检查,但LVGL可能没有现成的API,所以需要通过组合现有的函数来实现。例如,获取当前焦点对象,然后检查其类是否为lv_textarea_class。 总结步骤: 1. 获取当前活动的组(可能需要处理多个组的情况)。 2. 从组中获取当前焦点对象。 3. 检查该对象是否是textarea类型。 需要提醒用户注意组的正确配置,以及焦点管理的相关设置,确保textarea能够被正确聚焦。此外,如果用户没有使用组,或者焦点没有被正确管理,这个方法可能无效,需要检查他们的代码结构。 可能还需要考虑不同版本的LVGL在API上的差异,比如类检查的方式是否有变化。例如,使用lv_obj_get_class()函数,并与lv_textarea_class进行比较。 最后,提供一个代码示例,展示如何实现这个检查,并处理可能的异常情况,比如没有焦点对象的情况,或者对象类型不匹配的情况。</think>在 LVGL 中判断当前焦点控件是否是 `lv_textarea`,需通过**焦点组管理**和**对象类型验证**实现。以下是具体方法与示例代码: --- ### 一、核心步骤 1. **获取当前焦点控件** 通过 `lv_group_get_focused()` 从活动焦点组中提取焦点对象。 2. **验证控件类型** 使用 `lv_obj_check_type()` 或直接对比控件类指针,确认是否为 `lv_textarea`。 --- ### 二、代码实现 #### 1. 基础判断函数 ```c #include "lvgl.h" /** * @brief 判断当前焦点控件是否为 textarea * @return true: 是, false: 否或焦点为空 */ bool is_focused_textarea(void) { // 获取当前活动组(假设使用默认组) lv_group_t* act_group = lv_group_get_default(); if (!act_group) return false; // 获取焦点对象 lv_obj_t* focused_obj = lv_group_get_focused(act_group); if (!focused_obj) return false; // 方法1:通过类指针直接比较(推荐) if (lv_obj_get_class(focused_obj) == &lv_textarea_class) { return true; } // 方法2:使用类型检查函数(LVGL v8.3+) // return lv_obj_check_type(focused_obj, &lv_textarea_class); return false; } ``` #### 2. 使用示例 ```c // 在事件回调中动态判断 static void btn_event_cb(lv_event_t* e) { if (is_focused_textarea()) { lv_textarea_add_char(lv_group_get_focused(lv_group_get_default()), &#39;X&#39;); } else { lv_label_set_text(lv_event_get_current_target(e), "Focus not on textarea!"); } } ``` --- ### 三、关键机制说明 | 功能点 | 作用 | |-----------------------|--------------------------------------------------------------------| | `lv_group_get_default()` | 获取默认焦点组(需确保控件已通过 `lv_group_add_obj()` 加入组) | | `lv_group_get_focused()` | 从组中提取当前获得焦点的对象 | | `lv_obj_get_class()` | 获取对象的类指针,用于与 `lv_textarea_class` 对比 | | `lv_obj_check_type()` | 官方类型验证函数(需 LVGL 8.3 及以上版本) | --- ### 四、必要条件 1. **焦点组初始化** 确保目标 `lv_textarea` 已加入焦点组并启用焦点标志: ```c lv_group_t* group = lv_group_create(); lv_group_add_obj(group, textarea); // 将 textarea 加入组 lv_obj_add_flag(textarea, LV_OBJ_FLAG_CLICK_FOCUSABLE); // 允许点击聚焦 ``` 2. **输入设备绑定** 若使用物理按键/编码器,需将输入设备与组关联: ```c lv_indev_set_group(your_indev, group); // your_indev 为输入设备指针 ``` --- ### 五、高级场景 #### 1. 多组管理系统 若存在多个焦点组(如不同页面),需遍历所有组检查焦点: ```c lv_group_t* group = lv_group_get_next(NULL); // 获取第一个组 while (group) { lv_obj_t* focused = lv_group_get_focused(group); if (focused && lv_obj_check_type(focused, &lv_textarea_class)) { return true; } group = lv_group_get_next(group); // 获取下一个组 } return false; ``` #### 2. 实时监控焦点变化 通过**事件回调**监听焦点切换: ```c static void focus_event_cb(lv_event_t* e) { lv_obj_t* obj = lv_event_get_target(e); if (lv_obj_check_type(obj, &lv_textarea_class)) { LV_LOG_USER("Textarea focused!"); } } // 给所有 textarea 添加事件监听 lv_obj_add_event_cb(textarea, focus_event_cb, LV_EVENT_FOCUSED, NULL); ``` --- ### 六、常见问题 1. **始终返回 false** - 确认控件已加入焦点组 (`lv_group_add_obj()`) - 检查是否启用 `LV_OBJ_FLAG_CLICK_FOCUSABLE` 或 `LV_OBJ_FLAG_CHECKABLE` 2. **多输入设备干扰** 通过 `lv_indev_get_act()` 获取当前活跃输入设备,再关联到对应组。 通过上述方法,可精准判断当前焦点是否位于文本框控件,适用于**动态界面控制**、**输入法切换**等场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值