Get the value of a textbox during cut event

ぐ巨炮叔叔 提交于 2019-12-01 16:54:44

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)

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.

ars265

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!