Keydown event issue on HTML textbox

前端 未结 4 861
清歌不尽
清歌不尽 2021-01-15 03:05

I have a field that accepts the year, so I have created

input type=\"number\" 

and implemented keydown event to restrict

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-15 03:58

    HTML:

    
    

    jQuery:

    $(document).on('input', '.year', function(){
        var txt = $(this).val();
        if(isNaN(txt) || txt > 9999){
            $(this).val( $(this).data('temp') );
            return;
        }
        $(this).data('temp', txt);
    });
    

    JSFiddle

提交回复
热议问题