-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavaScript6.js
110 lines (94 loc) · 3.17 KB
/
JavaScript6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function updatePrice() {
let s = document.getElementsByName("productType");
let select = s[0];
let price = 0;
let prices = getPrices();
let priceIndex = parseInt(select.value) - 1;
if (priceIndex >= 0) {
price = prices.prodTypes[priceIndex];
}
let radioDiv = document.getElementById("radio-product-specifics");
radioDiv.style.display = (select.value === "3" ? "inline-block" : "none");
let radios = document.getElementsByName("radioOptions");
radios.forEach(function(radio) {
if (radio.checked) {
let optionPrice = prices.radioOptions[radio.value];
if (optionPrice !== undefined) {
price += optionPrice;
}
}
});
let checkDiv = document.getElementById("checkbox-product-specifics");
checkDiv.style.display = (select.value === "3" ? "none" : "inline-block");
let checkboxes = document.querySelectorAll("#checkbox-product-specifics input");
checkboxes.forEach(function(checkbox) {
if (checkbox.checked) {
let propPrice = prices.prodProperties[checkbox.name];
if (propPrice !== undefined) {
price += propPrice;
}
}
});
let prodPrice = document.getElementById("result");
prodPrice.innerHTML = price + " рублей";
}
function calculate() {
let regexp = /\D/g;
let productAmount = document.getElementsByName("product-amount")[0].value;
let r = document.getElementById("result");
if (Boolean(productAmount)) {
if (regexp.test(productAmount)) {
r.innerText = "Данные введены не корректно";
} else {
regexp = /\d/g;
productAmount = parseInt(productAmount.match(regexp).join(""));
let productPrice = r.innerText.match(/\d+/g);
r.innerText = `${productPrice * productAmount} рублей`;
}
}
return false;
}
function getPrices() {
return {
prodProperties: {
checkOptionsB: 5,
checkOptionsC: 10,
},
prodTypes: [100, 200, 150],
radioOptions: {
option2: 30,
option3: 90,
}
};
}
window.addEventListener("DOMContentLoaded", function (event) {
let radioDiv = document.getElementById("radio-product-specifics");
radioDiv.style.display = "none";
let s = document.getElementsByName("productType");
let select = s[0];
select.addEventListener("change", function(event) {
let target = event.target;
console.log(target.value);
updatePrice();
});
let radios = document.getElementsByName("radioOptions");
radios.forEach(function(radio) {
radio.addEventListener("change", function(event) {
let r = event.target;
console.log(r.value);
updatePrice();
});
});
let checkboxes = document.querySelectorAll("#checkbox-product-specifics input");
checkboxes.forEach(function(checkbox) {
checkbox.addEventListener("change", function(event) {
let c = event.target;
console.log(c.name);
console.log(c.value);
updatePrice();
});
});
updatePrice();
let button = document.getElementById("calculate-button");
button.addEventListener("click", calculate);
});