jquery 카 트 정보 등급 변화 실현

9788 단어 JavaScriptjquery
전 재 된 jquery 는 카 트 정보 등급 의 변 화 를 실현 하 는 예 로 일부 js 코드 를 수정 했다. 이 는 사용 되 지 않 은 몇 가지 방법 을 제거 하고 숫자 를 검증 하 는 방법 을 수정 했다. 원래 몇 개의 소수점 을 판단 할 수 없 었 는데 지금 은 정규 표현 식 으로 판단 한다.
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jQuery</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
	google.load("jquery", "1.2.6");
</script>
<script type="text/javascript" src="order.js"></script>
</head>
<body>
	<table id="order-table">
		<tr>
			<th>Product Name</th>
			<th>Quantity</th>
			<th>X</th>
			<th>Unit Price</th>
			<th>=</th>
			<th style="text-align: right;">Totals</th>
		</tr>
		<tr class="odd">
			<td class="product-title">www.corange.cn<em></em>
			</td>
			<td class="num-pallets"><input type="text" class="num-pallets-input" id="turface-pro-league-num-pallets"> </input></td>
			<td class="times">X</td>
			<td class="price-per-pallet">$<span>340</span>
			</td>
			<td class="equals">=</td>
			<td class="row-total"><input type="text" class="row-total-input" id="turface-pro-league-row-total" disabled="disabled"> </input></td>
		</tr>
		<tr class="even">
			<td class="product-title"><p>
					Turface&reg; Pro League Red - <em>Calcined Clay Top Dressinged</em>
				</p>
				<p>&nbsp;</p>
			</td>
			<td class="num-pallets"><input type="text" class="num-pallets-input" id="turface-pro-league-red-num-pallets"> </input></td>
			<td class="times">X</td>
			<td class="price-per-pallet">$<span>455</span>
			</td>
			<td class="equals">=</td>
			<td class="row-total"><input type="text" class="row-total-input" id="turface-pro-league-red-row-total" disabled="disabled"> </input>
			</td>
		</tr>
		<tr class="odd">
			<td class="product-title">Turface&reg; Quick Dry - <em>Calcined Clay Moisture Absorbent</em>
			</td>
			<td class="num-pallets"><input type="text" class="num-pallets-input" id="turface-quick-dry-num-pallets"> </input></td>
			<td class="times">X</td>
			<td class="price-per-pallet">$<span>100</span>
			</td>
			<td class="equals">=</td>
			<td class="row-total"><input type="text" class="row-total-input" id="turface-quick-dry-row-total" disabled="disabled"> </input></td>
		</tr>
		<tr class="even">
			<td class="product-title">Turface&reg; Mound Clay Red - <em>Virgin Red Clay</em>
			</td>
			<td class="num-pallets"><input type="text" class="num-pallets-input" id="turface-mound-clay-red-num-pallets"> </input></td>
			<td class="times">X</td>
			<td class="price-per-pallet">$<span>40</span>
			</td>
			<td class="equals">=</td>
			<td class="row-total"><input type="text" class="row-total-input" id="turface-mound-clay-red-row-total" disabled="disabled"> </input>
			</td>
		</tr>
		<tr class="odd">
			<td class="product-title">Red Infield Conditioner - <em>Vitrified Clay Top Dressing</em>
			</td>
			<td class="num-pallets"><input type="text" class="num-pallets-input" id="diamond-pro-red-num-pallets"> </input></td>
			<td class="times">X</td>
			<td class="price-per-pallet">$<span>35</span>
			</td>
			<td class="equals">=</td>
			<td class="row-total"><input type="text" class="row-total-input" id="diamond-pro-red-row-total" disabled="disabled"> </input></td>
		</tr>
		<tr class="even">
			<td class="product-title">Drying Agent - <em>Calcined Clay Moisture Absorbent</em>
			</td>
			<td class="num-pallets"><input type="text" class="num-pallets-input" id="diamond-pro-drying-agent-num-pallets"> </input></td>
			<td class="times">X</td>
			<td class="price-per-pallet">$<span>340</span>
			</td>
			<td class="equals">=</td>
			<td class="row-total"><input type="text" class="row-total-input" id="diamond-pro-drying-agent-row-total" disabled="disabled"> </input></td>
		</tr>
		<tr class="odd">
			<td class="product-title">Professional - <em>Calcined Clay Top Dressing</em>
			</td>
			<td class="num-pallets"><input type="text" class="num-pallets-input" id="diamond-pro-professional-num-pallets"> </input></td>
			<td class="times">X</td>
			<td class="price-per-pallet">$<span>75</span>
			</td>
			<td class="equals">=</td>
			<td class="row-total"><input type="text" class="row-total-input" id="diamond-pro-professional-row-total" disabled="disabled"> </input></td>
		</tr>
		<tr class="even">
			<td class="product-title">Top Dressing - <em>Calcined Clay Soil Conditioner</em>
			</td>
			<td class="num-pallets"><input type="text" class="num-pallets-input" id="diamond-pro-top-dressing-num-pallets"> </input></td>
			<td class="times">X</td>
			<td class="price-per-pallet">$<span>30</span>
			</td>
			<td class="equals">=</td>
			<td class="row-total"><input type="text" class="row-total-input" id="diamond-pro-top-dressing-row-total" disabled="disabled"> </input></td>
		</tr>
		<tr>
			<td colspan="6" style="text-align: right;">Product SUBTOTAL: <input type="text" class="total-box" id="product-subtotal" disabled="disabled"> </input></td>
		</tr>
	</table>
</body>
</html>

 
function isNumeric(str) {
	if (isInteger(str))
		return true;
	var re = /^[]{0,1}(\d+)[\.]+(\d+)$/;
	if (re.test(str)) {
		if (RegExp.$1 == 0 && RegExp.$2 == 0)
			return false;
		return true;
	} else {
		return false;
	}
}

function isInteger(str) {
	var regu = /^[]{0,1}[0-9]{1,}$/;
	return regu.test(str);
}

function calcProdSubTotal() {

	var prodSubTotal = 0;

	$(".row-total-input").each(function() {

		var valString = $(this).val() || 0;

		prodSubTotal += parseInt(valString);

	});

	$("#product-subtotal").val(prodSubTotal);

};

$(function() {

	$('.num-pallets-input').blur(
			function() {

				var $this = $(this);

				var numPallets = $this.val();
				var multiplier = $this.parent().parent().find("td.price-per-pallet span").text();

				if ((isNumeric(numPallets)) && (numPallets != '')) {

					var rowTotal = numPallets * multiplier;

					$this.css("background-color", "white").parent().parent()
							.find("td.row-total input").val(rowTotal);

				} else {

					$this.css("background-color", "#ffdcdc");
					$this.parent().parent().find("td.row-total input").val("");
				}
				;

				calcProdSubTotal();

			});

});

좋은 웹페이지 즐겨찾기