1

I'm building a Jquery/JS filtered data table. I have a search input field that on keypress fires an ajax request and then deals with the subsequent response.

All was working great, until I decided I wanted to include the backspace button press as an additional event, checking whether the field value was less than 1.

It worked fine until I added the second .on method that should fire exclusively on a backspace and nothing else.

If anyone asks, the ajaxify_laravel() function works fine and is not a problem in this script.

The result: A text box that will accept nothing other than backspace keypresses. How do I get this working to accept all key presses as normal but to handle backspace seperately? I thought I had it nailed but clearly I don't.

My code:

$('input[name="search"]').on('keypress', function() 
{

    if ($(this).val().length > 1)
    {               
        var request = {
            'filter' : $(this).attr('name'),
            'value'  : $(this).val()
        }, url = "/admin/user/view";

        ajaxify_laravel(url, request, build_user_html, failure);
    }

    return false;

}).on('keydown', function(e) {

    if (e.keyCode == 8 && $(this).val().length < 1)
    {
        var request = {
            'filter' : 'reset'
        }, url = "/admin/user/view";

        ajaxify_laravel(url, request, build_user_html, failure);

    }   
});
4

1 回答 1

1

看起来好像“按键”处理程序(至少有时)正在返回false。这将被解释为停止处理事件的命令——包括默认的“本机”行为。也就是说,它会导致按键被忽略。

于 2012-07-24T12:01:27.860 回答