You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在jQuery的DataTable-AltEditor中自动聚焦模态框首个可见输入框?

DataTable-AltEditor 模态框自动聚焦首个输入框解决方案

实现方案

由于AltEditor未提供原生autofocus配置项,可通过以下两种方式实现点击Add按钮后让name输入框自动聚焦:

方案1:监听Bootstrap模态框显示事件

AltEditor的模态框基于Bootstrap实现,利用其shown.bs.modal事件(模态框完全显示后触发)定位目标输入框:

// 监听AltEditor模态框显示完成事件
$(document).on('shown.bs.modal', '#altEditor-modal', function() {
  // 聚焦name输入框
  $(this).find('input[name="name"]').focus();
});

方案2:使用AltEditor内置回调

在AltEditor配置中通过onCreateForm回调,延迟触发聚焦(确保表单元素已完成渲染):

$('#example').DataTable({
  // 你的DataTable基础配置
  altEditor: {
    // 其他AltEditor配置项
    onCreateForm: function(form) {
      setTimeout(() => {
        form.find('input[name="name"]').focus();
      }, 100);
    }
  }
});

原方法失效原因

  • 列配置中设置autofocus:AltEditor的列选项未支持该属性,无对应处理逻辑。
  • 直接提前执行聚焦脚本:模态框未完成DOM渲染,目标输入框尚未存在,无法触发聚焦。

内容的提问来源于stack exchange,提问作者Sivapoornima Karuturi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 13:55:00