无法在使用脚本放置逗号的文本区域中退格
Can't backspace in textarea where it uses a script to place commas
我正在使用此脚本在您键入的每个项目后自动放置一个逗号 space,然后按 space 栏。
但出于某种原因,我完全不知道如何恢复space。
我希望它能够删除整个项目和每个后面的尾随逗号space。
$('#textarea').keyup(function(){
var str = this.value.replace(/(\w)[\s,]+(\w?)/g, ', ');
if (str!=this.value) this.value = str;
});
<textarea id='textarea'>item1, item2, item3</textarea>
它只启用退格键:
$('#textarea').keyup(function(e) {
if (e.which === 8) { // backspace detected
return;
}
var str = this.value.replace(/(\w)[\s,]+(\w?)/g, ', ');
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, ', ');
if (str!=this.value) this.value = str;
});
我正在使用此脚本在您键入的每个项目后自动放置一个逗号 space,然后按 space 栏。
但出于某种原因,我完全不知道如何恢复space。
我希望它能够删除整个项目和每个后面的尾随逗号space。
$('#textarea').keyup(function(){
var str = this.value.replace(/(\w)[\s,]+(\w?)/g, ', ');
if (str!=this.value) this.value = str;
});
<textarea id='textarea'>item1, item2, item3</textarea>
它只启用退格键:
$('#textarea').keyup(function(e) {
if (e.which === 8) { // backspace detected
return;
}
var str = this.value.replace(/(\w)[\s,]+(\w?)/g, ', ');
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, ', ');
if (str!=this.value) this.value = str;
});