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

FireFox下Semantic UI输入框退格与Ctrl+C复制功能失效问题

Fixing Firefox Compatibility Issues for Numeric Input with Copy Support

Hey there, I’ve run into this exact cross-browser quirk before—Firefox handles keyboard events a bit differently than Chrome, especially when it comes to functional keys like backspace and Ctrl+C. Let’s break down what’s probably going wrong with your current code and fix it step by step.

The Root Problem

Your existing code is likely blocking all non-numeric keystrokes without accounting for essential functional keys (backspace, delete, copy shortcuts) that Firefox treats differently in keypress/keyup events. For example, Firefox doesn’t fire the same keypress events for backspace as Chrome does, so your event handler might be accidentally swallowing those actions.

The Solution

We need to:

  • Allow critical functional keys (backspace, delete, arrow keys, Tab, Ctrl+C/V)
  • Clean up input values to only keep numbers and remove spaces
  • Ensure the logic works consistently across Chrome and Firefox

Here’s a revised version of your code that addresses these issues:

$("#checker").on("keydown keyup blur", function(event) {
  // Allow functional keys: backspace, delete, arrows, tab, Ctrl+C/V
  const allowedKeys = [8, 9, 46, 37, 38, 39, 40];
  const isCtrlKey = event.ctrlKey || event.metaKey; // Cover Windows Ctrl and Mac Cmd
  const isCopyPaste = isCtrlKey && (event.key === 'c' || event.key === 'v');

  // Skip processing if it's an allowed functional key or copy/paste shortcut
  if (allowedKeys.includes(event.keyCode) || isCopyPaste) {
    return;
  }

  // Block non-numeric characters on keydown to prevent them from being typed
  if (event.type === "keydown" && !/[0-9]/.test(event.key)) {
    event.preventDefault();
  }

  // Clean up the input value on keyup and blur to remove spaces and non-numbers
  if (event.type === "keyup" || event.type === "blur") {
    let cleanedValue = $(this).val().replace(/[^0-9]/g, '').replace(/\s/g, '');
    $(this).val(cleanedValue);
  }
});

Let’s Break Down the Changes

  • Switched to keydown instead of keypress: keydown is more consistent across browsers for detecting functional keys like backspace.
  • Explicitly allowed essential keys: We whitelist backspace, delete, arrow keys, Tab, and Ctrl/Cmd+C/V so these actions aren’t blocked.
  • Separate handling for keydown vs keyup/blur: On keydown, we block non-numeric characters before they’re typed. On keyup and blur, we clean up any remaining spaces or invalid characters (in case someone pasted non-numeric content).
  • Cross-platform shortcut support: Added metaKey to support Mac’s Cmd+C/V alongside Windows’ Ctrl+C/V.

Testing Tips

  • Try backspace/delete in Firefox—they should now work normally.
  • Use Ctrl+C (or Cmd+C on Mac) to copy the input value; it should behave just like in Chrome.
  • Paste both numeric and non-numeric content—only numbers will stay, and spaces will be stripped.

This should resolve the Firefox compatibility issues while keeping all your required features intact!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:50:37