【问题标题】:How to insert delimitter on key up after 7 letters/alphabet/numbers and new letters in new line- Jquery如何在新行中的 7 个字母/字母/数字和新字母之后在键上插入分隔符 - Jquery
【发布时间】:2016-03-13 04:19:35
【问题描述】:

如何在 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);
 }

【问题讨论】:

  • 你的意思是这样Fiddle?让我知道,如果这是正确的,我会发布一个完整的答案来解释解决方案
  • 是的,和 Fiddle 一样..@IanA

标签: javascript jquery css asp.net jquery-ui


【解决方案1】:

在您的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 2014-11-09
    • 2015-11-06
    相关资源
    最近更新 更多