Backspace when pressing "+"

2

In a given input , I allow the user to enter the content he / she understands, but pressing the + key will open a pop-up (which is working) with data coming from the database. What I am trying to do now is: When I press the + key I open the pop-up , and at the same time I give backspace to input to delete this + pressed in the input.

Input :

<input type="text" id="inputIdCliente" class="form-control input-sm" onkeypress = "openSearchCliente(event)"/></td>

Function that opens pop-up :

function openSearchCliente(e) {
        var unicode = e.keyCode ? e.keyCode : e.charCode;
        if (unicode == 43) {
            popupSearchCliente.Show(); //Abre pop up
            //Código para dar backspace na input (em falta)
        }
    }

backspace in input , I already looked for some things on the net and found no solution that replicates what I want to do ... < p>     

asked by anonymous 07.04.2014 / 17:34

3 answers

3

I'm not sure if it's the most appropriate way, but I think it should work:

var t = $(this).val();
$(this).val(t.substr(0, t.length-1));

#edit

Since you are using event keypress , try only with:

if (unicode == 43) {
    e.preventDefault();
}
    
07.04.2014 / 17:43
5

You can test the keys that are pressed with keyCode , for example:

$('input[type=text]').keydown(function(e) {
        // se a tecla pressionada for "+"
        if (e.keyCode == 107) {
          //abre o pop-up
          alert('abre pop-up');
          //limpa o "+" digitado
          $(this).val($(this).val().replace("+", ""));

        }
    });

Online Example

If you want to remove only the last typed character:

$('input[type=text]').keydown(function(e) {
        // se a tecla pressionada for "+"
        if (e.keyCode == 107) {
          //abre o pop-up
          alert('abre pop-up');
          //limpa o "+" digitado
          $(this).val($(this).val().substr(0, $(this).val().length-1));

        }
    });

Example 2

    
07.04.2014 / 19:56
0

Try to make a return false;

Example:

function openSearchCliente(e) {
        var unicode = e.keyCode ? e.keyCode : e.charCode;
        if (unicode == 43) {
            popupSearchCliente.Show(); //Abre pop up
            return false;
        }
    }
    
07.04.2014 / 17:53