JQuery案例:购物车加减

购物车加减

<head>
	<meta charset="UTF-8">
	<title>加减购物车</title>
	<style type="text/css">
		input {
			 35px;
			height: 35px;
			font-size: 20px;
			text-align: center;
		}
	</style>
	<script src="js/jquery-3.3.1.min.js"></script>
	<script>
		$(function() {
			$("input[value='-']").click(function() {
				var $txt = $(this).next("input");
				var num = parseInt($txt.val());
				if(num - 1 >= 0) {
					$txt.val(num - 1);
				}
			});
			$("input[value='+']").click(function() {
				var $txt = $(this).prev("input");
				var num = parseInt($txt.val());
				$txt.val(num + 1);
			});
		});
	</script>
</head>

<body>
	<p>
		<input type="button" value="-" />
		<input type="text" value="1" />
		<input type="button" value="+" />
	</p>

	<p>
		<input type="button" value="-" />
		<input type="text" value="1" />
		<input type="button" value="+" />
	</p>

</body>
原文地址:https://www.cnblogs.com/tigerlion/p/11178793.html