make an input only-numeric type on knockout

后端 未结 10 1046
情书的邮戳
情书的邮戳 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

    Create you data-bind pointing at your shiny new code:

    
    

    Shiny new knockout code:

    price = ko.observable();
    price.subscribe(function(newValue) {
        price = newValue.replace(/[\D\.]/g, '');
    });
    

    This means that every time you update the price, it will apply the logic in the function (in this case stripping out anything that isn't a number or a period), and apply it directly to the price. You can also add other validation or cool features here, like adding a currency sybmol at the start, keeping it to 2 decimal places, etc...

提交回复
热议问题