Page 1 of 1

How do I stop a form reducing to Zero value?

Posted: Tue Oct 13, 2015 7:47 am
by simonmlewis
Hi

I am trying to make this + - form prevent it going down to a zero (0).
Cannot quite see how to do that. I'm sure it can be done within its existing Javascript.

Can anyone offer some guidance please.

Code: Select all

<Script>$(window).load(function(){
jQuery(document).ready(function(){
    // This button will increment the value
    $('.qtyplus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
            $('input[name='+fieldName+']').val(currentVal + 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(0);
        }
    });
    // This button will decrement the value till 0
    $(".qtyminus").click(function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If it isn't undefined or its greater than 0
        if (!isNaN(currentVal) && currentVal > 0) {
            // Decrement one
            $('input[name='+fieldName+']').val(currentVal - 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(0);
        }
    });
});

});//]]>  
</script>

<form action='http://www.site.com/cart.asp' method='post' id='myform'>
<input type=hidden name=storeid value=11111>
<input type=hidden name=itemname2 value=trade>
<input type=hidden name=itemcode value='$row->romancode'>";
echo "
<div class='product_carton'>carton quantity</div>

<input type='button' value='-' class='qtyminus' field='quantity' />
    <input type='text' name='quantity' value='5' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity' />

Re: How do I stop a form reducing to Zero value?

Posted: Tue Oct 13, 2015 7:54 am
by Celauran
Without testing it, I assume it's going down to zero but not below?

Re: How do I stop a form reducing to Zero value?

Posted: Tue Oct 13, 2015 8:00 am
by simonmlewis
Correct.

Re: How do I stop a form reducing to Zero value?

Posted: Tue Oct 13, 2015 8:02 am
by simonmlewis
Hold on - what you jsut said made me wonder.
I just tweaked the final bit of code... to be 1, rather than 0. Easy I suppose.

Re: How do I stop a form reducing to Zero value?

Posted: Tue Oct 13, 2015 8:04 am
by simonmlewis
And readonly on the field to stop it being typed in.