我想在 GridView 文本框中添加点和逗号,例如我在 asp.net gridview 中有一个文本框字段,它是货币字段。我输入了 1234 美元和 50 美分。如果我写 1234 => 我的按键,它应该在 COMMA 1.234 之后返回 1.234。我按逗号“,”)它应该停止“。”智能感知 1.234,50 美分。但它不能正常工作。
如果我开始在 keyup 上写
应该开始改变它。它应该开始将输入更改为:(在写作时)
1234 美元 50 分输入 (123450) => 1.234,50
1234567 美元 75 美分(1234567(你可以加逗号。它不应该阻止)75) => 1.234.567,75)
我的 C# 代码:
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" />
关于javascript - 如何在 GridView 文本框中制作点和逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39202372/