gpt4 book ai didi

javascript - 如何在打字时设置文本框格式?

转载 作者:行者123 更新时间:2023-11-28 10:48:31 25 4
gpt4 key购买 nike

当用户在文本框中键入内容时,我希望它使用小数来格式化自己。

例如,如果用户输入 10000,我希望他在输入时显示为 10,000

最佳答案

给你。处理退格键/删除键使其变得特别具有挑战性。蛮好玩的! :-)

我已将其添加到我的在线脚本组合中:rack.pub .

function toast(a,b){b||(b=2750);var c={message:a,timeout:b};snackbarContainer.MaterialSnackbar.showSnackbar(c)}var doc=document,textArea=doc.getElementById("area"),numArray=[],backArray=[],num="",numF="",regx="",thisChar="",lastChar="",str="",index=0;window.snackbarContainer=doc.querySelector("#toast"),textArea.addEventListener("keydown",function(){var a=event.keyCode;if(str=this.value,(8==a||46==a)&&(backArray=[],index=str.length-1,lastChar=str.charAt(index),!isNaN(lastChar)||","==lastChar))for(var b=str.length-1;b>=0;b--){if(" "==str.charAt(b))return;if(isNaN(str.charAt(b))&&","!=str.charAt(b))return;backArray.push(str.charAt(b))}if(32==a&&backArray[1]){var c=backArray.reverse().slice(0,-1).join(""),d=c.replace(/\,/g,""),e=Number(d).toLocaleString().toString(),f=str.lastIndexOf(c);f>=0&&f+c.length>=str.length&&(str=str.substring(0,f)+e),this.value=str}}),textArea.addEventListener("keypress",function(){if(thisChar=this.value.slice(-1),isNaN(thisChar)){num=numArray.join(""),numArray=[],numF=Number(num).toLocaleString().toString(),regx=num+"(?!.*"+num+")",regx=new RegExp(regx);var a=this.value.replace(regx,numF);this.value=a}else{if(" "==thisChar){num=numArray.join(""),numArray=[],numF=Number(num).toLocaleString().toString(),regx=num+"(?!.*"+num+")",regx=new RegExp(regx);var a=this.value.replace(regx,numF);return void(this.value=a)}numArray.push(thisChar)}});
        html body {
font-family: 'Roboto', sans-serif;
background: #f5f5f5;
}
.text-right{
text-align:right;
}
.text-center{
text-align:center;
}
.text-left{
text-align:left;
}
.thin{
font-weight: 100;
}
.heading{
font-size:3em;
}
.subtitle{
margin-top: -16px;
}
#submit{
margin-top:10px;
}
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.2.0/material.indigo-pink.min.css">
<link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">

<div class="demo-layout-transparent mdl-layout mdl-js-layout">
<main class="mdl-layout__content page-content">

<section class="mdl-grid">
<div class="mdl-layout-spacer"></div>
<div class="mdl-cell mdl-cell--4-col">
<h1 class="mdl-color-text--indigo-900 text-right thin">commas.js demo</h1>
<h6 class="mdl-color-text--indigo-500 text-right subtitle">
JavaScript to automatically add commas to numbers in a text area
</h6>
</div>
<div class="mdl-layout-spacer"></div>
</section>

<section class="mdl-grid">
<div class="mdl-layout-spacer"></div>
<div class="mdl-cell mdl-cell--4-col">
<h6 class="mdl-color-text--black thin">
Basically it auto formats numbers as you type in the text area. Give it a try.
</h6>
</div>
<div class="mdl-layout-spacer"></div>
</section>

<section class="mdl-grid">
<div class="mdl-layout-spacer"></div>
<div class="mdl-textfield mdl-js-textfield mdl-cell mdl-cell--4-col">
<textarea class="mdl-textfield__input" type="text" rows= "3" id="area" ></textarea>
<label class="mdl-textfield__label" for="area">Type Here...</label>
</div>
<div class="mdl-layout-spacer"></div>
</section>

</main>
</div>

<!-- IE Compatibility shims DO NOT DELETE-->
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.2/html5shiv.min.js""></script>
<![endif]-->

<!--[if IE]>
<script src="//cdnjs.cloudflare.com/ajax/libs/es5-shim/4.1.7/es5-shim.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/classlist/2014.01.31/classList.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/selectivizr/1.0.2/selectivizr-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/flexie/1.0.3/flexie.min.js"></script>
<link href="../assets/ie.css" rel="stylesheet">
<![endif]-->
<!-- end shims -->
<script defer src="https://code.getmdl.io/1.2.0/material.min.js"></script>

关于javascript - 如何在打字时设置文本框格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com