How to restrict max value on html5 number input on manual entry

后端 未结 2 480
滥情空心
滥情空心 2021-01-28 10:46

Free jqgrid column is defined to use html5 number input type like

{ name: \"amount\", width: 62, template: \"number\",
             formatter: \"number\", format         


        
2条回答
  •  情话喂你
    2021-01-28 11:38

    Create validation on input with jQuery.

    Event listeners are attached on edit click, and removed on save click. I used setTimeout, to be synchronized with free-jqgrid elements manipulation - as the proper solution will be to extend free-jqgrid functionality

    function restrictMax(){
    
        var max = parseFloat($(this).attr('max'))
        var value = parseFloat($(this).val())
        if($(this).val() > max){
            $(this).val(max)
        }
    }
    setTimeout(function(){
        $('.fa-pencil').click(function(){ //click on edit
            var thatParent = $(this).closest('tr')
            setTimeout(function(){
    
                thatParent.find('input[type="number"]').on('input',restrictMax)
            },0);
        })
        $('.fa-floppy-o').click(function(){ //click on save
            var thatParent = $(this).closest('tr')
            setTimeout(function(){
                thatParent.find('input[type="number"]').off('input',restrictMax)
            },0)
        })
    
    },0)
    

    http://jsfiddle.net/jhckz7rr/6/

提交回复
热议问题