admin管理员组

文章数量:1399474

I need to find a way to detect if a space was deleted or backspaced, and run a function if that is the case. I am working on this in JavaScript / jQuery.

I know I can get the delete or backspace key press by using:

$(this).keyup(function(event) {
        event.keyCode

However, I do not know how to tell if the delete or backspace mand removed a space?

Very appreciative for any suggestions.

I need to find a way to detect if a space was deleted or backspaced, and run a function if that is the case. I am working on this in JavaScript / jQuery.

I know I can get the delete or backspace key press by using:

$(this).keyup(function(event) {
        event.keyCode

However, I do not know how to tell if the delete or backspace mand removed a space?

Very appreciative for any suggestions.

Share Improve this question edited Aug 24, 2015 at 21:26 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Dec 5, 2012 at 17:03 BaxterBaxter 5,84526 gold badges75 silver badges109 bronze badges 8
  • When you detect a backspace or a delete key pressed you check the current position of the caret in your input element and check if before/after it is a space that will be deleted? – koopajah Commented Dec 5, 2012 at 17:07
  • do you need detect only space or it might be mixed with text? – dmi3y Commented Dec 5, 2012 at 17:08
  • @dmi3y I only need to detect a space being removed. I have something else that handles multiple character deletion. – Baxter Commented Dec 5, 2012 at 17:11
  • what if a space AND a character was removed? why would you need to detect these two things separately? wouldn't it make more sense to detect both at once? – Kevin B Commented Dec 5, 2012 at 17:13
  • What exactly is this for? can you add some context? – Kevin B Commented Dec 5, 2012 at 17:14
 |  Show 3 more ments

4 Answers 4

Reset to default 2

See here: http://jsfiddle/Txseh/

(function(){
    var currentWhitespaceCount;

    $("input").keyup(function(e){
        var newCount = ($(this).val().match(/\s/g) || []).length;

        if (newCount < currentWhitespaceCount)
            alert("You removed one or more spaces, fool.");

        currentWhitespaceCount = newCount;
    });
})();​

It tracks the current number of whitespace characters in the input, and if ever the number goes down, it alerts(or does whatever you want).

Cache the value beforehand (set a value on keypress) and pare with the value after keypress. That is the only way to know with certainty that one or more spaces has been removed. Any checking of keys relies on you being able to work out what possible keys could achieve the removal of a space, and will likely leave holes.

As an example, selecting the final letter of a word and the space following it, if we press the last letter it will remove the space. But the key pressed is not backspace or delete.

Bind to the keydown and pare the value from before and after to see if it reduced in size.

$(input).keydown(function(){
    var currVal = this.value, self = this;
    setTimeout(function(){
        if ( currVal.length > self.value.length ) {
            console.log(currVal.length - self.value.length + " characters have been removed.");
        }
    },0);
});

http://jsfiddle/ymhjA/1/

Updated sample:

$("input").keydown(function() {
    var currVal = this.value,
        self = this;
    setTimeout(function() {
        if (currVal.length - self.value.length === 1) {
            var origVal = $.grep(currVal.split(""),function(val){
                return val === " ";
            });
            var newVal = $.grep(self.value.split(""),function(val){
                return val === " ";
            });
            if ( origVal.length != newVal.length ) {
                console.log("a space was removed");
            }
        }
    }, 0);
});​

http://jsfiddle/ymhjA/4/

actually here is my code http://jsbin./atuwez/3/edit

 var input = $('#input'),
     afterLength,
     beforeLength;

input.on({
  'keydown': function () {
    beforeLength = input.val().split(/\s/).length;
  },
  'keyup': function(event) {
    var key = event.keyCode || event.charCode;

    if( key == 8 || key == 46 ) {
          afterLength = input.val().split(/\s/).length;
          console.log(beforeLength == afterLength);
    }
  }

});

本文标签: javascriptHow to detect that a space was backspaced or deletedStack Overflow