如何在GridView文本框中产生点和逗号的感觉
本文关键字:感觉 GridView 文本 | 更新日期: 2023-09-27 18:04:42
我想把点和逗号的感觉在GridView文本框,例如,我有一个文本框字段在asp.net GridView它是钱字段。我有一个输入1234美元和50美分。如果我写1234 =>我的按键,它应该在逗号后返回1.234。我按下逗号",")它应该停止"."智能感知1.234,50美分.但它不能正常工作。
如果我开始写
在keyup应该开始改变它。它应该开始将输入改为:(while writing)
$ $ $ $ $ $ $ $ $ $ $ $ $ $
(你可以加逗号。它不应该阻止)75)=> 1.234.567,75)
My c# Code:
private void GvRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtVeri = (TextBox)e.Row.FindControl("txtVeri");
txtVeri.Attributes.Add("onkeyup", "InsertComma(this.id)");
}
}
我的JS代码:
function InsertComma(veriId) {
console.log("çalışıyor");
var txtObj = document.getElementById(veriId);
var txtVal = replaceAll(txtObj.value, '.', '');
//alert(txtObj.value);
if (txtObj.value != "") {
var newVal = "";
for (var i = 0; i < txtVal.length; i++) {
//alert(txtVal.substring(i, 1));
newVal = newVal + txtVal.substring(i, i + 1);
if ((i + 1) % 3 == 0 && i != 0 && i + 1 < txtVal.length) {
newVal = newVal + ".";
}
}
txtObj.value = newVal;
}
}
function replaceAll(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'), with_this);
}
根据您的要求,请看一下这个例子
$(document).ready(function () {
$(".btnSubmit").click(function () {
var txtObj = $("#txtInput").val();
if (txtObj.length != "") {
var newVal = "";
var arr = txtObj.split(' ');
if (arr[0].length >= 4) {
var doller = arr[0];
var cents = arr[2];
var decimal = ".";
var position = doller.length - 3
var output = [doller.slice(0, position), decimal, doller.slice(position)].join('') + "," + cents;
$("#txtInput").val(output);
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="txtInput" value="1234 dollers 50 cents" /><br />
<input type="submit" id="btnSubmit" class="btnSubmit" />
下面的代码根据你的注释
$(document).ready(function () {
$(".btnSubmit").click(function () {
var txtObj = $("#txtInput").val();
if (txtObj.length != "") {
var newVal = "";
var doller = txtObj;
if (doller.length >= 4) {
var decimal = ".";
var comma = ",";
var posDecimal = doller.length - (doller.length - 1);
var posComm = doller.length - 1;
var output = [doller.slice(0, posDecimal), decimal, doller.slice(posDecimal)].join('');
var FinalOutput = [output.slice(0, posComm), comma, output.slice(posComm)].join('');
$("#txtInput").val(FinalOutput)
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" id="txtInput" value="123450" /><br /><br />
<input type="submit" id="btnSubmit" class="btnSubmit" />