make an input only-numeric type on knockout

后端 未结 10 979
情书的邮戳
情书的邮戳 2020-11-30 06:15

i read many tutorials but i dont know how to do this, this is the input

input(type=\"text\",name=\"price\",id=\"price\"data-bind=\"text: price,valueUpdate:[\         


        
10条回答
  •  独厮守ぢ
    2020-11-30 06:34

    With the help of "keydown" event we can restrict other key's in text box which should hold numeric values.

    $(document).ready(function(){                
            $("selector").on("keydown", function (e) {
                //numbers, delete, backspace, arrows
                var validKeyCodes = [8, 9, 37, 38, 39, 40, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
                if (!($.inArray(e.keyCode, validKeyCodes) >= 0))
                        e.preventDefault();                 
            });           
        });
    

提交回复
热议问题