How to increase the value of a quantity field with jQuery?

前端 未结 8 1165
北荒
北荒 2021-01-07 05:25

I have a form with some quantity field and a plus and minus sign on each side,

    
product1
8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-07 05:51

    I think this should do it:

    $('#add').click(function (e) {
        var quant = $(this).next('input');
        quant.val(parseInt(quant.val(), 10) + 1);
    };
    $('#minus').click(function (e) {
        var quant = $(this).prev('input');
        quant.val(parseInt(quant.val(), 10) - 1);
        if (parseInt(quant.val(), 10) < 0)
        { quant.val(0); }
    };
    

    This does however depend on the relative positioning of the controls not changing.

提交回复
热议问题