How to restrict number of characters that can be entered in HTML5 number input field on iPhone

前端 未结 10 1538
野趣味
野趣味 2020-12-14 02:19

It seems that neither of the \"maxlength\", \"min\" or \"max\" HTML attributes have the desired effect on iPhone for the following markup:

 

        
10条回答
  •  情歌与酒
    2020-12-14 02:32

    Here a more optimized jQuery version that caches the selectors. It also uses the maxlength attribute that is not supported by input type number.

    // limits the input based on the maxlength attribute, this is by default no supported by input type number
    $("input[type=number]").on('keydown keyup',function(){ 
        var $that = $(this),
        maxlength = $that.attr('maxlength')
        if($.isNumeric(maxlength)){
            $that.val($that.val().substr(0, maxlength));
        };
    });
    

提交回复
热议问题