【问题标题】:Can't backspace in textarea where it uses a script to place commas无法在使用脚本放置逗号的文本区域中退格
【发布时间】:2018-12-09 22:49:59
【问题描述】:

我正在使用此脚本在您键入的每个项目后自动放置一个逗号空格并按空格键。

但由于某种原因,我根本不知道如何让它退格。

我希望它能够删除整个项目和每个退格的尾随逗号。

   $('#textarea').keyup(function(){
   var str = this.value.replace(/(\w)[\s,]+(\w?)/g, '$1, $2');
   if (str!=this.value) this.value = str; 
   });

<textarea id='textarea'>item1, item2, item3</textarea>

【问题讨论】:

    标签: javascript comma backspace


    【解决方案1】:

    它只是启用退格:

    $('#textarea').keyup(function(e) {
      if (e.which === 8) { // backspace detected
          return;
      }
      var str = this.value.replace(/(\w)[\s,]+(\w?)/g, '$1, $2');
      if (str!=this.value) this.value = str; 
    });
    

    编辑:完整版:

    $('#textarea').keyup(function(e){
        var str = this.value;
        if (e.which === 8) {
            var tmp = str.split(', ');
            tmp.splice(tmp.length - 1, 1);
            str = tmp.join(', ');
            this.value = str;
            return;
        }
        str = str.replace(/(\w)[\s,]+(\w?)/g, '$1, $2');
        if (str!=this.value) this.value = str; 
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      相关资源
      最近更新 更多