【发布时间】:2017-05-30 12:24:21
【问题描述】:
我有一个表单,您可以在其中通过降价或单击表情符号选择器来附加表情符号。当点击选择器中的表情符号时,它会运行以下 jQuery 代码:
$('.emoji-container').click(function () {
var target = $(this).closest('.emoji-container');
var emojiName = target.data('clipboard');
var activityLength = $('.input-field').val().length;
var emojiLength = (emojiName).length;
var totalLength = activityLength + emojiLength;
if (totalLength < 90) {
$('.clipboard-emoji').html("Added: '" + emojiName + "'");
$('.input-field').append(emojiName + " ");
console.log(activityLength, '+', emojiLength, '=', totalLength)
} else {
$('.clipboard-emoji').html("Max. characters reached, can't copy");
console.log('full')
}
});
所以每个人的工作都很完美,但是当我删除输入字段中的字符时,有足够的时间再次添加另一个表情符号,它不会将其附加到输入字段。我的表情符号被添加为这样的降价::smiley:
我的控制台:
0 "+" 19 "=" 19 // at this point I add the first emoji
20 "+" 19 "=" 39
40 "+" 19 "=" 59
60 "+" 19 "=" 79
full // at this point the max is reached and I clear the input again
0 "+" 19 "=" 19 // at this point I cleared the whole input field
0 "+" 10 "=" 10 // as you can see the input length stays 0
0 "+" 11 "=" 11
0 "+" 7 "=" 7
0 "+" 8 "=" 8
我不明白为什么在删除字符或输入字段中的整个输入时它不会再次添加它们
【问题讨论】:
-
创建一个小提琴,以便我们检查和纠正
-
如何清除 $('.input-field')?
-
@techLove Backspace :p
-
尝试使用
$('.emoji-container').on('input', function () { }而不是点击事件,可能会解决它。为了活着回响,你能解决你的问题吗?我们可以帮忙吗? -
@AlivetoDie 添加
标签: javascript jquery html css