disable enter key on specific textboxes

前端 未结 9 853
北荒
北荒 2020-12-30 18:25

I was looking for a way to do this, got a few scripts, but none of them is working for me.

When I hit enter in my textboxes, it shouldn\'t do anything.

I hav

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-30 19:10

    Works for me:

    $('#comment').keypress(function(event) {
        if (event.which == 13) {
            event.preventDefault();
        }
    });
    

    http://jsfiddle.net/esEUm/

    UPDATE

    In case you're trying to prevent the submitting of the parent form rather than a line break (which only makes sense, if you're using a textarea, which you apparently aren't doing?) you might want to check this question: Disable the enter key on a jquery-powered form

提交回复
热议问题