Нужна помощь с калькулятором
Здравствуйте, я совсем новичок, пытаюсь разбираться в js.
Проблема такая, нужно в простой калькулятор с чекбоксами добавить 2 группы radio, так, чтобы у каждой группы был свой value, и они прибавлялись бы к общей сумме. По отдельности все работает, я только не знаю, как их соединить. Буду рада любой помощи.
<SCRIPT LANGUAGE="JavaScript">
function getRadioGroupValue(radioGroupObj)
{
var val = 0;
for (var i=0; i < radioGroupObj.length; i++)
if (radioGroupObj[i].checked){
val+=+radioGroupObj[i].value;
}
var total=1000+val;
document.getElementById("result").innerHTML=total;
}
function price(form) {
if (form.exp1.checked){
var exp1=parseFloat(form.exp1.value);
}
else exp1=0;
if (form.exp2.checked){
var exp2=parseFloat(form.exp2.value);
}
else exp2=0;
if (form.exp3.checked){
var exp3=parseFloat(form.exp3.value);
}
else exp3=0;
var total=exp1+exp2+exp3;
document.getElementById("total_price").innerHTML=total;
}
</SCRIPT><center>
<form name="form" onclick="price(this)">
<table width="350" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td align="center" colspan="5"><b>Список</b></td>
</tr>
<tr>
<td> </td>
<td align="left" colspan="3"><input type="checkbox" value="6000" name="exp1">Тест 1</td>
<td>- 6000</td>
</tr>
<tr>
<td> </td>
<td align="left" colspan="3"><input type="checkbox" value="8000" name="exp2">Тест 2</td>
<td>- 8000</td>
</tr>
<tr>
<td> </td>
<td align="left" colspan="3"><input type="checkbox" value="12000" name="exp3">Тест 3</td>
<td>- 12000</td>
</tr>
<form name="radioForm" onclick="getRadioGroupValue(this)">
<tr>
<td align="left" colspan="4"> <b>Время</b></td>
<td> </td>
<tr>
<td> </td>
<td align="left"><input type="radio" name="group1" value="1">z</td>
<td><input type="radio" name="group1" value="2">a</td>
<td><input type="radio" name="group1" value="3">b</td>
<td>
<div id="result">0</div>
</td>
</tr><br>
<td align="left" colspan="4"> <b>Количество</b></td>
<td> </td>
<tr>
<td> </td>
<td align="left"><input type="radio" name="group2" value="1">z</td>
<td><input type="radio" name="group2" value="2">a</td>
<td><input type="radio" name="group2" value="3">b</td>
<td>
<div id="result">0</div>
</td>
</tr>
</form>
<tr>
<td> </td>
<td align="center" colspan="3"><input type="button" value="заказать" onclick="tf1.style.display = 'inline';"> <b>Итого:</b></td>
<td align="center" id="total_price" style="color: #993300;font-weight:bold; font-size:14px">0</td>
</tr>
</tbody>
</table>
</form>
</center>
|
Цитата:
|
Я взяла два разных кода, один от обычного простого калькулятора с чекбоксами, другой от калькулятора с радио, и пытаюсь добавить блоки radio в свой готовый калькулятор. Простите мою косноязычность, надеюсь, объяснила.
|
Цитата:
Цитата:
А просто копировать какие-то куски - это не есть "разбираться"... |
К сожалению, до самостоятельного написания мне еще далеко. Пока смотрю, как другие пишут и стараюсь вникнуть.
|
extravert, твоя задачка букварская... На них и учатся что-то делать. Т.ч. самое время начать. ;)
|
ksa, ну подсказка бы не помешала, а то у меня уже ступор начался и ощущение собственного дуболомства (
|
Цитата:
Цитата:
|
Попробовала сама. Объясните, пожалуйста, где моя ошибка.
<script type="text/javascript">
function calc() {
var type_exp1 = document.getElementById("type_exp1");
var type_exp2 = document.getElementById("type_exp2");
var type_exp3 = document.getElementById("type_exp3");
var type_exp4 = document.getElementById("type_exp4");
for (var i=0; i < type_exp4.length; i++)
if (type_exp4[i].checked){
type_exp4+=+type_exp4[i].value;
}
var price_exp4= type_exp4;
document.getElementById("result_exp4").innerHTML=price_exp4;
//Result
var result = document.getElementById("result");
var price_exp1 = 0;
var price_exp2 = 0;
var price_exp3 = 0;
var price_exp4 = 0;
var price = 0;
price_exp1 += (type_exp1.checked == true) ? parseInt(type_exp1.value) : 0;
price_exp2 += (type_exp2.checked == true) ? parseInt(type_exp2.value) : 0;
price_exp3 += (type_exp3.checked == true) ? parseInt(type_exp3.value) : 0;
price_exp4 += (type_exp4.checked == true) ? parseInt(type_exp4.value) : 0;
price= price_exp1 + price_exp2 + price_exp3 + price_exp4;
result_exp1.innerHTML = price_exp1;
result_exp2.innerHTML = price_exp2;
result_exp3.innerHTML = price_exp3;
result_exp4.innerHTML = price_exp4;
result.innerHTML = price;
}
</script>
<table width="600">
<tr>
<td colspan="3" width="500"><input type="checkbox" onchange="calc()" value="1500" id="type_exp1" />test1</td>
<td width="200"><span id="result_exp1">0</span> руб.</td>
</tr>
<tr>
<td colspan="3" width="500"><input type="checkbox" onchange="calc()" value="1500" id="type_exp2" />test2</td>
<td width="200"><span id="result_exp2">0</span> руб.</td>
</tr>
<tr>
<td colspan="3" width="500"><input type="checkbox" onchange="calc()" value="1500" id="type_exp3" />test3</td>
<td width="200"><span id="result_exp3">0</span> руб.</td>
</tr>
<tr>
<td width="200"><input type="radio" onchange="calc()" value="1500" name="type_exp4" id="type_exp4" />test4</td>
<td width="200"><input type="radio" onchange="calc()" value="1500" name="type_exp4" id="type_exp4" />test4</td>
<td width="200"><input type="radio" onchange="calc()" value="1500" name="type_exp4" id="type_exp4" />test4</td>
<td width="200"><span id="result_exp4">0</span> руб.</td>
</tr>
<!-- Итог -->
<tr>
<td colspan="3" width="500" class="td_result">ИТОГО:</td>
<td class="td_result"><span id="result">0</span> руб.</td>
</tr>
</table>
|
Ребята, ну что же вы! Помогите нубу, пожалуйста!
|
| Часовой пояс GMT +3, время: 03:42. |