jQuery Method for Quickly Realizing Addition and Subtraction of Commodity

  • 2021-07-16 01:43:53
  • OfStack

No more nonsense, just post the code for everyone. The specific code is as follows:


<!--  Quickly realize quantity addition and subtraction  -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
// Get a text box object 
var t = $("#text_box");
// The initialization quantity is 1, And failure reduction 
$('#min').attr('disabled',true);
 // Quantity increase operation 
 $("#add").click(function(){ 
  //  To get val Add absolute values to avoid negative numbers 
  t.val(Math.abs(parseInt(t.val()))+1);
  if (parseInt(t.val())!=1){
  $('#min').attr('disabled',false);
  };
 }) 
 // Quantity reduction operation 
 $("#min").click(function(){
 t.val(Math.abs(parseInt(t.val()))-1);
 if (parseInt(t.val())==1){
 $('#min').attr('disabled',true);
 };
 })
});
</script> 
</head> 
<body> 
<input id="min" name="" type="button" value="-" /> 
<input id="text_box" name="" type="text" value="1" style="width:30px;text-align: center"/> 
<input id="add" name="" type="button" value="+" /> 
</body> 
</html>

To add and subtract quantity is as simple as that!


Related articles: