我在asp.net中有货币文本框 我用:
<script type="text/javascript">
function Comma(Num) { //function to add commas to textboxes
Num += '';
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
</script>
和
<asp:TextBox ID="amountTextBox" runat="server" onkeyup = "javascript:this.value=Comma(this.value);"></asp:TextBox>
它有效并且没有任何问题,但我希望当用户按*时,而不是*在文本框中写'000'。怎么做这个工作?
答案 0 :(得分:2)
我希望这就是你需要的东西
<script type="text/javascript">
function Comma(Num) { //function to add commas to textboxes
if (Num.indexOf("*") != -1) {
Num = document.getElementById('amountTextBox').value.substring(0, Num.indexOf("*")) + '000';
}
Num += '';
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
Num = Num.replace(',', ''); Num = Num.replace(',', ''); Num = Num.replace(',', '');
x = Num.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1))
x1 = x1.replace(rgx, '$1' + ',' + '$2');
return x1 + x2;
}
</script>