Using jquery to achieve increment and decrement of input values

Source: Internet
Author: User

On many e-commerce websites, where the shopping cart is on the page, when it comes to the number of items, a + sign button and-number button are provided to increase 1 and minus 1, and only input values are allowed. Bootstrap Touchspin This plugin is written for this requirement.

First introduce the necessary CSS and JS files.

    <link href= "bootstrap/css/bootstrap.min.css" rel= "stylesheet"/>
    <link href= "css/jquery.bootstrap-touchspin.min.css" rel= "stylesheet"/>
    <script src= "scripts/jquery-2.1.3.min.js" ></script>
    <script src= "bootstrap/js/bootstrap.min.js" ></script>
    <script src= "scripts/jquery.bootstrap-touchspin.min.js" ></script>

-Control the accuracy and self-decrement of the numerical value

    <br/>
    <div style= "margin-left:10px;" >
        class= "form-horizontal" role= "form" >
            class= "form-group" >
                class= "col-xs-2" >
                    <input id= "demo1" type= "textvalue=""name="demo1class= "form-control"/>
                </div>
            </div>
        </form>
    </div>
    <script type= "text/javascript" >
        $ (function () {
            $ ("input[name= ' demo1 ')". Touchspin ({
                min:0,
                MAX:100,
                step:0.1,//increment or decrement
                Precision
                Boostat:5,
                Maxboostedstep:10,
                Suffix
            });
        });
    </script>

Click the + sign button to increase by 0.1
Click the-Number button to subtract 0.1
2 decimal places reserved
Minimum allowable value 0.00
Maximum allowable value 100.00
Only allow values to be entered, otherwise lose focus show minimum 0.00

-only allow integers starting from 1, which is also a common practice for shopping cart pages

   <div style= "margin-left:10px;" >
        class= "form-horizontal" role= "form" >
            class= "form-group" >
                class= "col-xs-2" >
                    <input id= "demo2" type= "textvalue="1"name="demo2class= " form-control"/>
                </div>
            </div>
        </form>
    </div>
    <script type= "text/javascript" >
        $ (function () {
            $ ("input[name= ' Demo2 ')". Touchspin ({
                Min:1,
                MAX:100,
                Step:1//increment or decrement
            });
        });
    </script>   

Click the + sign button to increase by 1
Click the-Number button to subtract 1
Minimum allowable value 1
Maximum allowable value 100
Only allow values to be entered, otherwise lose focus show minimum 1

For other uses, refer to the documentation.

Using jquery to achieve increment and decrement of input values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.