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

如何在Firefox浏览器中禁用日期输入框的键盘输入功能?

解决Firefox中日期输入框允许键盘输入的问题

以下是几种可行的解决方案,既能保留日历选择功能,又能阻止键盘输入:

方案1:阻止键盘输入与粘贴事件

通过监听键盘和粘贴事件,直接阻止所有手动输入操作:

<input
  className="date__input"
  type="date"
  key={t.id}
  value={t.factStart?.toLocaleDateString('en-CA')}
  onChange={(e) => handleChangeInput(e, index, 'factStart')}
  onKeyDown={(e) => e.preventDefault()}
  onPaste={(e) => e.preventDefault()}
/>

如果onKeyDown在Firefox中无效,可尝试替换为onKeyPress事件:

<input
  className="date__input"
  type="date"
  key={t.id}
  value={t.factStart?.toLocaleDateString('en-CA')}
  onChange={(e) => handleChangeInput(e, index, 'factStart')}
  onKeyPress={(e) => e.preventDefault()}
  onPaste={(e) => e.preventDefault()}
/>

方案2:输入时重置为原始值

监听输入事件,若检测到非日历选择的内容变更,立刻将输入框值重置为原始日期:

<input
  className="date__input"
  type="date"
  key={t.id}
  value={t.factStart?.toLocaleDateString('en-CA')}
  onChange={(e) => handleChangeInput(e, index, 'factStart')}
  onInput={(e) => {
    const originalDate = t.factStart?.toLocaleDateString('en-CA') || '';
    if (e.target.value !== originalDate) {
      e.target.value = originalDate;
    }
  }}
/>

方案3:结合readonly与点击触发日历

通过临时移除readonly属性触发日历弹窗,完成选择后重新添加readonly阻止键盘输入:

const handleDateClick = (e) => {
  e.target.removeAttribute('readonly');
  e.target.click();
  setTimeout(() => {
    e.target.setAttribute('readonly', 'readonly');
  }, 0);
};

<input
  className="date__input"
  type="date"
  key={t.id}
  value={t.factStart?.toLocaleDateString('en-CA')}
  onChange={(e) => handleChangeInput(e, index, 'factStart')}
  onClick={handleDateClick}
  readOnly
/>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 16:42:45