How do I listen for step up event for input type=“number”

前端 未结 5 1805
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 11:45

I want to be able to listen to step UP (increment) and step down events with jQuery. (currently I can only understand how to lis

5条回答
  •  感情败类
    2021-01-12 12:25

    There is no event for up and down. You can use change event

    $(".counter").change(function () {
       alert($(this).val());      
    })
    

    DEMO

    You can try something like, You can store previous value and compare with currently value and identify up or down

    $(".counter").change(function () {
        if ($(this).data('old-value') < $(this).val()) {
            alert('Alert up');
        } else {
            alert('Alert dowm');
        }
        $(this).data('old-value', $(this).val());
    })
    

    DEMO

提交回复
热议问题