I am trying to make textbox similar to the Twitter, for this i have written code for
我正在尝试让文本框与Twitter类似,为此我编写了代码。
- Word Count
- 字数
- Used Events Change, Keyup and Paste
- 使用事件更改、键合和粘贴
Keyup and Change Events are working fine but paste event is little bit strange, when i paste something in textarea the word count doesn't change at that moment, after some debugging i found that paste event fires up before pasting something on textbox. I don't know how they handle this in Twitter. Here is my code : events:
Keyup和Change事件运行得很好,但是粘贴事件有点奇怪,当我在textarea中粘贴一些东西时,单词计数在那个时候不会改变,在一些调试之后,我发现粘贴事件在粘贴文本框之前会触发。我不知道他们在推特上是怎么处理这件事的。以下是我的代码:事件:
'click #textboxId' : 'submitQuestion'
'keyup #textboxId' : 'wordCounter'
'change #textboxId' : 'wordCounter'
'paste #textboxId' : 'wordCounter'
wordCounter: ->
#Code for Word Count#
Due to pre-paste nature of paste event the work count doesn't changes on that instance.
Your suggestion and help will be appreciated, Thanks for your time.
由于粘贴事件的预粘贴性质,该实例上的工作计数不会改变。谢谢您的建议和帮助,谢谢您的时间。
4 个解决方案
#1
8
See this example.
看到这个例子。
http://jsfiddle.net/urEhK/1
$('textarea').bind('input propertychange', function() {
$('#output').html($(this).val().length + ' characters');
});
That behavior was very weird. You would think that one of those events would catch this properly? I was surprised there weren't more answers to this via Google.
这种行为很奇怪。你会认为其中的一个事件会很好地抓住这一点吗?我很惊讶没有更多的答案通过谷歌。
#2
2
function update()
{
alert($textbox.val().length);
}
var $textbox = $('input');
$textbox.bind('keyup change cut paste', function(){
update(); //code to count or do something else
});
// And this line is to catch the browser paste event
$textbox.bind('input paste',function(e){ setTimeout( update, 250); });
#3
0
You should now use on() instead of bind() see this post. It's also unnecessary to create a named function, you can just create a anonymous function.
现在应该使用on()而不是bind(),请参阅本文。也无需创建命名函数,只需创建一个匿名函数即可。
$('#pasteElement').on('paste', function() {
setTimeout(function(){
alert($('#pasteElement').val());
}, 100);
});
#4
0
You can do one thing that firstly get original data . then you can get the event paste data and add them.
你可以做一件事,首先得到原始数据。然后您可以获得事件粘贴数据并添加它们。
$("#id").on("paste keypress ",function(event){
// eg. you want to get last lenght
var length=$("#id").val().length;
if(event.type == "paste"){
//then you can get paste event data.=
length+=event.originalEvent.clipboardData.getData('text').length;
}
});`
#1
8
See this example.
看到这个例子。
http://jsfiddle.net/urEhK/1
$('textarea').bind('input propertychange', function() {
$('#output').html($(this).val().length + ' characters');
});
That behavior was very weird. You would think that one of those events would catch this properly? I was surprised there weren't more answers to this via Google.
这种行为很奇怪。你会认为其中的一个事件会很好地抓住这一点吗?我很惊讶没有更多的答案通过谷歌。
#2
2
function update()
{
alert($textbox.val().length);
}
var $textbox = $('input');
$textbox.bind('keyup change cut paste', function(){
update(); //code to count or do something else
});
// And this line is to catch the browser paste event
$textbox.bind('input paste',function(e){ setTimeout( update, 250); });
#3
0
You should now use on() instead of bind() see this post. It's also unnecessary to create a named function, you can just create a anonymous function.
现在应该使用on()而不是bind(),请参阅本文。也无需创建命名函数,只需创建一个匿名函数即可。
$('#pasteElement').on('paste', function() {
setTimeout(function(){
alert($('#pasteElement').val());
}, 100);
});
#4
0
You can do one thing that firstly get original data . then you can get the event paste data and add them.
你可以做一件事,首先得到原始数据。然后您可以获得事件粘贴数据并添加它们。
$("#id").on("paste keypress ",function(event){
// eg. you want to get last lenght
var length=$("#id").val().length;
if(event.type == "paste"){
//then you can get paste event data.=
length+=event.originalEvent.clipboardData.getData('text').length;
}
});`