Methods for implementing input numerical increment and decrement using jquery _jquery

Source: Internet
Author: User

The example in this article describes the method of using jquery to implement the input numerical increment and decrement. Share to everyone for your reference. The specific analysis is as follows:

In many electric business website, in the shopping cart on the page, involves the quantity of goods, will provide a + Number button and-number button to achieve 1 and minus 1, and only allow input values. Bootstrap Touchspin This plugin is written for this requirement. (Bootstrap touchspin This plugin click here to download this website.) )

First introduce the necessary CSS and JS files.

Copy Code code as follows:
<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>

The precision of the control numerical value and the self reducing quantity

Copy Code code as follows:
<br/>
<div style= "margin-left:10px;" >
<form class= "form-horizontal" role= "form" >
<div class= "Form-group" >
<div class= "Col-xs-2" >
<input id= "Demo1" type= "value=" name= "Demo1" class= "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
Decimals:2,//precision
Boostat:5,
Maxboostedstep:10,
Postfix: '% '//suffix
});
});
</script>

Click the + Number button to increase your own 0.1
Click the-Number button to subtract 0.1
Keep 2-digit decimal point
Minimum allowable value 0.00
Maximum allowable value 100.00
Only allow values to be entered, otherwise lose focus display minimum 0.00

Second, only allow starting from 1 integers, which is also the shopping cart page common practice

Copy Code code as follows:
<div style= "margin-left:10px;" >
<form class= "form-horizontal" role= "form" >
<div class= "Form-group" >
<div class= "Col-xs-2" >
<input id= "Demo2" type= "text" value= "1" name= "Demo2" class= "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 + Number button to increase your own 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 display minimum 1

For other uses, interested friends can refer to the relevant documentation.

I hope this article will help you with your jquery programming.

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.