Get the value of a textbox during cut event

前端 未结 3 1870
我寻月下人不归
我寻月下人不归 2021-01-18 05:33

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 t

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-18 06:04

    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)

提交回复
热议问题