如何使数字键盘除键充当制表符,数字键盘小数充当退格键

我需要 / (除法)充当制表键(在输入之间切换)和 . (十进制)使用纯 JavaScript 或 JavaScript 和 jQuery、CSS 和 HTML 充当退格键(删除文本框中的字符)。我怎样才能做到这一点?


也许有这样的脚本?


document.addEventListener('keyup', function (e) {

      if (e.keyCode == 111) {

        var e2 = jQuery.Event("keydown");

        e2.which = 8; 

        e2.keyCode = 8;

        $("input").trigger(e2);

      }

    });

这对于标签的事情


document.addEventListener('keyup', function (e3) {

      if (e3.keyCode == 110) {

        var e4 = jQuery.Event("keydown");

        e4.which = 9; 

        e4.keyCode = 9;

        $("input").trigger(e4);

      }

    });


饮歌长啸
浏览 135回答 1
1回答

江户川乱折腾

考虑以下示例。$(function() {&nbsp; function alterText(evt) {&nbsp; &nbsp; var c = evt.which;&nbsp; &nbsp; var t = $(evt.target);&nbsp; &nbsp; if (c !== 46 && c !== 47) {&nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; evt.preventDefault();&nbsp; &nbsp; if (c == 46) {&nbsp; &nbsp; &nbsp; t.val(t.val().slice(0, -1));&nbsp; &nbsp; }&nbsp; &nbsp; if (c == 47) {&nbsp; &nbsp; &nbsp; t.next().focus();&nbsp; &nbsp; }&nbsp; &nbsp; return false;&nbsp; }&nbsp; $("input").keypress(alterText);});<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><input type="text" id="part-1" /><input type="text" id="part-2" /><input type="text" id="part-3" />你可以使用.keypress(),在这里查看更多:Simulate press tab key with jQuery如果which是 46 (&nbsp;.) 或 47 (&nbsp;/) 我们可以模拟特定的操作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript