如何在keyup事件中输入7个字母之后的逗号,下面是我的代码,我可以在7个字母后得到逗号,但是当它转到下一行时它不能正常工作。
<asp:TextBox ID ="txtbillno" runat="server" onkeyup="InsertComma();" TextMode="MultiLine"></asp:TextBox>
function InsertComma() {
var txtObj = document.getElementById('<%=txtbillno.ClientID %>');
var txtVal = replaceAll(txtObj.value, ',', '');
if (txtObj.value != "") {
var newVal = "";
for (var i = 0; i < txtVal.length; i++) {
newVal = newVal + txtVal.substring(i, i + 1);
if ((i + 1) % 7 == 0 && i != 0) {
newVal = newVal + "," + "\n";
}
}
txtObj.value = newVal;
}
}
function replaceAll(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'), with_this);
}
答案 0 :(得分:0)
在您的InsertComma
功能中,您正确删除了逗号:
var txtVal = replaceAll(txtObj.value, ',', '');
但是txtVal
仍然包含换行符号(\ n),所以我添加了一行来删除它们:
txtVal = replaceAll(txtVal, '\n', '');
完整的InsertComma
方法是:
function InsertComma() {
var txtObj = document.getElementById('txtbillno');
// Remove commas
var txtVal = replaceAll(txtObj.value, ',', '');
// Remove new line characters
txtVal = replaceAll(txtVal, '\n', '');
if (txtObj.value != "") {
var newVal = "";
// Append commas
for (var i = 0; i < txtVal.length; i++) {
newVal = newVal + txtVal.substring(i, i + 1);
if ((i + 1) % 7 == 0 && i != 0) {
newVal = newVal + "," + "\n";
}
}
txtObj.value = newVal;
}
}
可以找到一个工作示例here