简体   繁体   中英

JavaScript increment and decrement BigInt value in input type number max

I have HTML code:

<input type="number" max>

So, when I paste bigint into this input and try to increment or decrement, input value becomes in scientific notation format. I need to remove scientific notation format and get incremented or decremented value of this bigint number using JavaScript.

Internally I've a feeling an input type number is going to be using the Number type, and not the bigint. You will likely need to just use a normal string input, and handle the increment, decrement manually..

Below is an example, you will probably want to extend to handle keypress for number etc, you could alter the look and feel to make it appear like a number input etc.

 const [iBig, iNorm] = document.querySelectorAll('input'); iNorm.value = 10000000000000000000000; const [btnUp, btnDown] = [...document.querySelectorAll('button')]; btnUp.addEventListener('click', () => { iBig.value = (BigInt(iBig.value) + 1n).toString(); }); btnDown.addEventListener('click', () => { iBig.value = (BigInt(iBig.value) - 1n).toString(); });
 <p>Handle our bigint's manually.</p> <input value="10000000000000000000000" style="width: 200px"/> <button>⬆</button> <button>⬇</button> <br/> <p>Normal input.. with such a big number does not work</p> <input value="" style="width: 200px" type="number"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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