혹시 이놈을 자동합계하려면 스크립트를 어찌 써야 할까요 ?
본문
<tr>
<th scope="row"><label for="wr8">상품명</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[0]; ?>" class="frm_input" size="50"></td>
<th scope="row"><label for="wr8">수량</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[1]; ?>" class="frm_input"></td>
<th scope="row"><label for="wr8">개당가격</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[2]; ?>" class="frm_input"></td>
</tr>
<tr>
<th scope="row"><label for="wr8">상품명</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[3]; ?>" class="frm_input" size="50"></td>
<th scope="row"><label for="wr8">수량</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[4]; ?>" class="frm_input"></td>
<th scope="row"><label for="wr8">개당가격</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[5]; ?>" class="frm_input"></td>
</tr>
<tr>
<th scope="row"><label for="wr8">상품명</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[6]; ?>" class="frm_input" size="50"></td>
<th scope="row"><label for="wr8">수량</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[7]; ?>" class="frm_input"></td>
<th scope="row"><label for="wr8">개당가격</label></th>
<td><input type="text" name="wr8[]" value="<?php echo $wr8[8]; ?>" class="frm_input"></td>
</tr>
프로그램에 무뇌인이 회사에서 조금 소스 수정만 할 줄 아는데 이것좀 도와주세요..
스크립트 소스좀 도와주세요..ㅠㅠ
!-->
답변 4
아래처럼 가능은 합니다.
하지만 중요한 것은 서버측 검증처리 이고
서버측 검증처리가 필요하다고 하면
클라이언트로 꾸민 효과의 결과와 서버로 넘어간 결과의 데이터는
일치해야 하는게 중요한 것이구요.
현재 소스는 name 이 모두 동일 하므로
서버측 검증이 애매할 가능성이 큽니다.
참고 용으로 이런 방법으로 가능하다 정도만 가늠하시기 바랍니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>title</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
function test(objid) {
var rtn = 0;
var tmp = 0;
$("#" + objid + ' input[type="text"]').each(function (index) {
switch (index % 3) {
case 0: // 상품명
tmp = 0;
break;
case 1: // 수량
tmp += Number($(this).val());
break;
case 2: // 개당가격
rtn += tmp * Number($(this).val());
break;
}
});
$("#prices_all").html(rtn);
}
</script>
</head>
<body>
<table id="prices">
<tr>
<th scope="row"><label for="wr8">상품명</label></th>
<td><input type="text" name="wr8[]" value="aaa" class="frm_input" size="50"></td>
<th scope="row"><label for="wr8">수량</label></th>
<td><input type="text" name="wr8[]" value="1" class="frm_input"></td>
<th scope="row"><label for="wr8">개당가격</label></th>
<td><input type="text" name="wr8[]" value="10" class="frm_input"></td>
</tr>
<tr>
<th scope="row"><label for="wr8">상품명</label></th>
<td><input type="text" name="wr8[]" value="bbb" class="frm_input" size="50"></td>
<th scope="row"><label for="wr8">수량</label></th>
<td><input type="text" name="wr8[]" value="3" class="frm_input"></td>
<th scope="row"><label for="wr8">개당가격</label></th>
<td><input type="text" name="wr8[]" value="20" class="frm_input"></td>
</tr>
<tr>
<th scope="row"><label for="wr8">상품명</label></th>
<td><input type="text" name="wr8[]" value="ccc" class="frm_input" size="50"></td>
<th scope="row"><label for="wr8">수량</label></th>
<td><input type="text" name="wr8[]" value="6" class="frm_input"></td>
<th scope="row"><label for="wr8">개당가격</label></th>
<td><input type="text" name="wr8[]" value="30" class="frm_input"></td>
</tr>
</table>
<input type="button" onclick="test('prices')" value="합계" />
: <span id="prices_all"></span>
</body>
</html>
jquery로 처리하시되 해당 id값을 뒤에 배열 말고 상품에 따른 유니크한 번호 값을 만들어서 그에 따른 값을 합산 될수 있도록 처리해 주셔야 할듯 보입니다.
지금은 상품명 수량 개당 가격이 wr8[] 이렇게 되어 있어서 이걸로는 처리하기는 어려운 부분이 있습니다.
<script type="text/javascript">
$(function() {
$("#cal").on("click", function() {
var ea = price = total = 0;
$("table tr").each(function() {
ea = $(this).find("input[name='wr8[]']:eq(1)").val();
price = $(this).find("input[name='wr8[]']:eq(2)").val();
total += ea * price;
});
$("#result").text(total);
});
});
</script>
http://nyaongii.dothome.co.kr/temp/wrid_205082.html
억지로 맞춰서 하면 이런 식으로도 가능하긴 합니다.
근데 어지간하면 개수, 가격 이런 건 다른 name을 쓰시는 게 좋습니다.
!-->불가능한것은 아니지만
계산방식을 제공한 제작의뢰에 적합해보이네요
답변을 작성하시기 전에 로그인 해주세요.