简体   繁体   English

(已解决)二进制/十六进制(或任何其他基数)的行为类似于输入类型=数字

[英](solved) Binary/hex (or any other radixes) to act like input type=number

Using any browser that isn't a mobile device, number input has many features of changing it besides typing a number into it: -Pressing Up on the arrow keys increases the value, and down arrow keys decreases it.使用任何不是移动设备的浏览器,除了在其中输入数字外,数字输入还有许多更改它的功能: - 按向上箭头键增加值,向下箭头键减小它。 If held down, it will increase or decrease by step , default at 1 (integer) rapidly.如果按住不放,它将step增加或减少,默认为 1(整数)快速。 Additionally, on most browsers, when selecting it will show two additional buttons to the right to increment and decrement it (and holding down those as well will increase/decrease rapidly):此外,在大多数浏览器上,选择它将显示两个额外的按钮,以增量和减少它(并保持这些按钮,也将迅速增加/减少):

在此处输入图像描述

However, this only works with decimal numbers.但是,这只适用于十进制数。 There isn't a method of having a field to enter, say hex numbers.没有一种方法可以输入一个字段,比如十六进制数字。 There exist a text version ( <input type="text"> ) that allows the user to enter any character in it, and a JavaScript can parse the text into a number (or bigint) using regex prior conversion.存在允许用户在其中输入任何字符的文本版本 ( <input type="text"> ),并且 JavaScript 可以使用正则表达式预先转换将文本解析为数字(或 bigint)。 But that will not behave like a number since it is a “general-purpose” text input.但这不会像数字一样,因为它是“通用”文本输入。

So my question is, how do I make an input, that is a text, to behave like a number but accepts a given radix (I don't know the code, but it reacts to up/down, and features increase/decrease, and can be held down for continuous change)?所以我的问题是,我如何进行输入,即文本,表现得像一个数字但接受给定的基数(我不知道代码,但它对向上/向下做出反应,并且特征增加/减少,并且可以按住以进行连续更改)? Having the ability to increase/decrease a hex number would be great for making HTML that does things that are programmer-like stuff such as color math or something related to software data.具有增加/减少十六进制数的能力对于制作 HTML 非常有用,它可以执行类似于程序员的事情,例如颜色数学或与软件数据相关的事情。

Solved (but partially).解决(但部分)。 Here is a template, this only works with bigint unless you edit it to support number (double-precision floating point).这是一个模板,它仅适用于 bigint,除非您对其进行编辑以支持number (双精度浮点)。 I don't think the up and down arrow to the right of the input is even possible:我认为输入右侧的向上和向下箭头甚至是不可能的:

<input type="text" id="tester" class="HexInput"><br>
<input type="text" id="tester2" class="HexInput"><br>
<input type="text" id="tester2" class="SomethingElse"><br>

<script>
        window.onload = function(){    
            var FocusedElement = document.activeElement;
            if(FocusedElement.addEventListener ) {
                FocusedElement.addEventListener('keydown',this.keyHandler,false);
            } else if(FocusedElement.attachEvent ) {
                FocusedElement.attachEvent('onkeydown',this.keyHandler);
            }
        }

        function keyHandler(e) {
            if (((e.keyCode == 38)||e.keyCode == 40)&&(document.activeElement.className == "HexInput")) {
                var target;
                var InputNumber = BigInt(CorrectHexBigIntValue(document.activeElement.value))
                if (!e) var e = window.event;
                if (e.target) target = e.target;
                else if (e.srcElement) target = e.srcElement;
        
                if(e.keyCode == 38) {
                    InputNumber++
                } else if(e.keyCode == 40) {
                    if (InputNumber > 0n) {
                        InputNumber--
                    }
                }
                document.activeElement.value = InputNumber.toString(16).toUpperCase()
            }
        }
    function CorrectHexBigIntValue(String) {
        if ((/^([0-9]|[A-F]|[a-f])+$/).test(String) == false) {
            String = 0n
        }
        return CanConvertHexStringToBigInt = BigInt("0x" + String)
    }
</script>

But there is one problem: Pressing up always brings the caret (blinking text cursor) to the beginning of the line.但是有一个问题:按向上总是会将插入符号(闪烁的文本光标)带到行首。 On a number input, this always bring the caret to the end, on both pressing up and down.在数字输入上,这总是将插入符号带到最后,同时按下向上和向下。

YES.是的。 It is solved, Sorry for adding another answer.已解决,抱歉添加另一个答案。 stackoverflow is having a bug that wouldn't let me add more text in the previous post, This here is meant to include an improved version: I found a way to prevent the caret from going to the beginning of the line by using preventDefault(): stackoverflow 有一个错误,不允许我在上一篇文章中添加更多文本,这意味着包括一个改进的版本:我找到了一种方法来防止插入符号进入行首,方法是使用 preventDefault() :

<input type="text" id="tester" class="HexNumberInput" onchange="code()"><br>
<input type="text" id="tester2" class="HexNumberInput" onchange="code()"><br>
<input type="text" id="tester3" class="somethingelse" onchange="code()"><br>
<script>
    window.onload = function(){
        var FocusedElement = document.activeElement;
        if(FocusedElement.addEventListener ) {
            FocusedElement.addEventListener('keydown',this.keyHandler,false);
        } else if(FocusedElement.attachEvent ) {
            FocusedElement.attachEvent('onkeydown',this.keyHandler);
        }
    }

    function keyHandler(e) {
        if (((e.keyCode == 38)||e.keyCode == 40)&&(document.activeElement.className == "HexNumberInput")) {
            var target;
            var InputNumber = BigInt(CorrectHexBigIntValue(document.activeElement.value))
            if (!e) var e = window.event;
            if (e.target) target = e.target;
            else if (e.srcElement) target = e.srcElement;
    
            if(e.keyCode == 38) {
                InputNumber++
                e.preventDefault()  //Prevents the caret (blinking text cursor) from being placed at the beginning of the line.
            } else if(e.keyCode == 40) {
                if (InputNumber > 0n) {
                    InputNumber--
                }
            }
            document.activeElement.value = InputNumber.toString(16).toUpperCase()
            document.activeElement.setSelectionRange(document.activeElement.value.length, document.activeElement.value.length)
        }
    }
    function code() {
        var b = 1 + 1
    }
    function CorrectHexBigIntValue(String) {
        //This converts the user's input hex string (negative numbers not allowed)
        //to BigInt.
        if ((/^([0-9]|[A-F]|[a-f])+$/).test(String) == false) {
            String = 0n
        }
        return CanConvertHexStringToBigInt = BigInt("0x" + String)
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM