셀렉트방식을 체크박스로 변경
본문
write.skin.php 에서 사용하는 합산 소스입니다.
여분필드에 여러개의 값을 셀렉트하여 explode로 더해서 저장시키고 불러오는 방식인데요.
체크박스 방식으로 바꾸고 싶은데 input을 어떻게 표현해야하고 스크립트를 어떻게 변경해야할지 몰라서요.
조언 부탁드려봅니다.
//더해라
<script language='javascript'>
function calc()
{
if (document.getElementById("sell1").value && document.getElementById("sell2").value && document.getElementById("sell3").value && document.getElementById("sell4").value && document.getElementById("sell5").value) {
document.getElementById('sell6').value =parseInt(document.getElementById('sell1').value) + parseInt(document.getElementById('sell2').value) + parseInt(document.getElementById('sell3').value) + parseInt(document.getElementById('sell4').value) + parseInt(document.getElementById('sell5').value);
} else {
document.getElementById('sell6').value="";
}
}
</script>
//저장해라
<form name='sumform'>
<?
$wr5 = explode("|",$write['wr_5']);
$sell1 = $wr5[0];
$sell2 = $wr5[1];
$sell3 = $wr5[2];
$sell4 = $wr5[3];
$sell5 = $wr5[4];
$sell6 = $wr5[5];
?>
<ul >
<li>
<label>1</label>
<select name='sell1' id='sell1' onchange="calc()">
<option value="">선택하세요.</option>
<option value="10" <?php echo ($sell1 == "10") ? "selected" : "";?>>10</option>
<option value="8" <?php echo ($sell1 == "8") ? "selected" : "";?>>8</option>
<option value="6" <?php echo ($sell1 == "6") ? "selected" : "";?>>6</option>
<option value="4" <?php echo ($sell1 == "4") ? "selected" : "";?>>4</option>
<option value="2" <?php echo ($sell1 == "2") ? "selected" : "";?>>2</option>
</select>
</li>
<li>
<label>2</label>
<select name='sell2' id='sell2' onchange="calc()">
<option value="">선택하세요.</option>
<option value="10" <?php echo ($sell2 == "10") ? "selected" : "";?>>10</option>
<option value="8" <?php echo ($sell2 == "8") ? "selected" : "";?>>8</option>
<option value="6" <?php echo ($sell2 == "6") ? "selected" : "";?>>6</option>
<option value="4" <?php echo ($sell2 == "4") ? "selected" : "";?>>4</option>
<option value="2" <?php echo ($sell2 == "2") ? "selected" : "";?>>2</option>
</select>
</li>
<li>
<label>3</label>
<select name='sell3' id='sell3' onchange="calc()">
<option value="">선택하세요.</option>
<option value="10" <?php echo ($sell3 == "10") ? "selected" : "";?>>10</option>
<option value="8" <?php echo ($sell3 == "8") ? "selected" : "";?>>8</option>
<option value="6" <?php echo ($sell3 == "6") ? "selected" : "";?>>6</option>
<option value="4" <?php echo ($sell3 == "4") ? "selected" : "";?>>4</option>
<option value="2" <?php echo ($sell3 == "2") ? "selected" : "";?>>2</option>
</select>
</li>
<li>
<label>4</label>
<select name='sell4' id='sell4' onchange="calc()">
<option value="">선택하세요.</option>
<option value="10" <?php echo ($sell4 == "10") ? "selected" : "";?>>10</option>
<option value="8" <?php echo ($sell4 == "8") ? "selected" : "";?>>8</option>
<option value="6" <?php echo ($sell4 == "6") ? "selected" : "";?>>6</option>
<option value="4" <?php echo ($sell4 == "4") ? "selected" : "";?>>4</option>
<option value="2" <?php echo ($sell4 == "2") ? "selected" : "";?>>2</option>
</select>
</li>
<li>
<label>5</label>
<select name='sell5' id='sell5' onchange="calc()">
<option value="">선택하세요.</option>
<option value="10" <?php echo ($sell5 == "10") ? "selected" : "";?>>10</option>
<option value="8" <?php echo ($sell5 == "8") ? "selected" : "";?>>8</option>
<option value="6" <?php echo ($sell5 == "6") ? "selected" : "";?>>6</option>
<option value="4" <?php echo ($sell5 == "4") ? "selected" : "";?>>4</option>
<option value="2" <?php echo ($sell5 == "2") ? "selected" : "";?>>2</option>
</select>
</li>
<li>합계: <input type='text' name='sell6' id='sell6' class="frm_input" value="<?=$sell6?>"> 입니다.</li>
</ul>
</td>
</tr>
</form>
답변 2
radio 가 아니고 checkbox 맞죠?
radio 1개만 선택 가능
checkbox 여러개 선택 가능
<script language="javascript">
function itemSum(frm)
{
var sum = 0;
var count = frm.chkbox.length;
for(var i=0; i < count; i++ ){
if( frm.chkbox[i].checked == true ){
sum += parseInt(frm.chkbox[i].value);
}
}
frm.total_sum.value = sum;
}
</script>
이 스트립트로
<input name="chkbox" type="checkbox" value="1000" onClick="itemSum(this.form);" <?php echo ($buy1 == "1000") ? "selected" : "";?>>
이런식으로 처리했습니다. 감사합니다.
답변을 작성하시기 전에 로그인 해주세요.