在剪切事件期间获取文本框的值 [英] Get the value of a textbox during cut event

查看:101
本文介绍了在剪切事件期间获取文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文本框中捕获了cut事件(jquery)。我想要的是在切换事件触发期间获取文本框上的文本。

I have trapped the cut event (jquery) on a textbox. What I want is to get the text on the textbox during the cut event is triggered.

我尝试通过evt.originalEvent.clipboardData访问用户剪切的数据。 getData('text')但返回undefined。

I've tried accessing the data the user cut via evt.originalEvent.clipboardData.getData('text') but returns undefined.

我的目标是知道用户是否剪切了所有文本(文本框现在是空的)。

My goal is to know if the user cut all the text (textbox is now empty) or not.

提前致谢

推荐答案

你可以 setTimeout ,持续时间为0,计划立即执行的函数。好处是该函数将在文本被剪切后执行,因此您可以检查您的textarea是否为空(这意味着用户已经剪切了所有文本):

You can setTimeout with a duration of 0, which schedules a function for immediate execution. The nice thing is that the function will execute once the text has already been cut, so you can check then if your textarea is empty (which would mean that the user has cut all the text):

var ta = $('#YOUR_TEXTAREA');
ta.bind('cut', function() {
    setTimeout(function(){
        if (!ta.val()) { 
            // user cut the whole text.
        }
    },0);
});

您可能还想在 setTimeout 在文本被剪切之前测试textarea中是否有任何文本(如果用户按下Ctrl ^ X而没有选择任何文本,则剪切事件仍会触发)

You might also want to add a check before the setTimeout to test whether there is any text in the textarea before the text gets cut (if the user presses Ctrl^X without any text being selected, the cut event still triggers)

这篇关于在剪切事件期间获取文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆