我想在GridView文本框中添加点和逗号感觉,例如我在asp.net gridview中有一个文本框字段,它是money field。我输入1234美元和50美分。如果我写1234 =>我的按键它应该在COMMA 1.234后返回它1.234。我正在按逗号","它应该停止"。" intellisense 1.234,50分。但它不能正常工作。
如果我开始写关键字应该开始改变它。它应该开始改变输入:(写作时)
1234美元50美分输入(123450)=> 1.234,50
1234567美元75美分(1234567(你可以把comma.it不应该阻止)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);
}
答案 0 :(得分:1)
根据您的要求,请查看此示例
$(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);
}
}
});
});
&#13;
<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" />
&#13;
根据您的评论代码
$(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)
}
}
});
});
&#13;
<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" />
&#13;