在剪切事件期间获取文本框的值

发布于 2025-01-07 12:52:25 字数 184 浏览 2 评论 0原文

我已将剪切事件(jquery)捕获在文本框上。我想要的是在触发剪切事件期间获取文本框上的文本。

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

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

提前致谢

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.

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.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本王不退位尔等都是臣 2025-01-14 12:52:25

您可以将持续时间设为 0 的 setTimeout 来安排函数立即执行。好处是,一旦文本被剪切,该函数就会执行,因此您可以检查文本区域是否为空(这意味着用户已剪切所有文本):

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

您可能还想在之前添加检查setTimeout 用于在文本被剪切之前测试文本区域中是否有文本(如果用户在没有选择任何文本的情况下按下 Ctrl^X,剪切事件仍然会触发)

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);
});

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)

野鹿林 2025-01-14 12:52:25

希望我的理解是正确的:

在 jQuery 中,您可以使用类似的方法来查看每个用户的按键上的文本框是否为空:

var txt;
$('#textbox_ID').live('keyup', function() {
    txt = $(this).val().length;
    if(txt < 1) {
        alert("textbox is empty");
    }
});

这应该可行,因为每次用户释放按键并使文本框聚焦时,它都会检查它是否为空。

Hope I got you right:

In jQuery you could use something like this to see if a textbox is empty on every user's keyup:

var txt;
$('#textbox_ID').live('keyup', function() {
    txt = $(this).val().length;
    if(txt < 1) {
        alert("textbox is empty");
    }
});

This should work, because everytime the user releases a key and has the textbox focused, it checks if it's empty.

橘亓 2025-01-14 12:52:25

我建议看看这个, JavaScript 在粘贴事件上获取剪贴板数据(跨浏览器),它用于粘贴事件,但我确信您可以执行类似的操作并将当前值与剪贴板上的值进行比较,如果它们完全相同,则输入将为空,否则不是。

I would suggest looking at this, JavaScript get clipboard data on paste event (Cross browser), it is for the paste event but I'm sure you could do something similar and compare the current value to that on the clipboard, if they are exactly the same than the input would be empty, otherwise not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文