英文:
Calculate with Javascript / HTML-Dropdowns
问题
Sure, here's the translated content you requested:
我有一个订单的价格存储在一个PHP变量中,假设是$price。这个$price是每日$rate * $rental_days的总和。接下来的步骤中,可以通过HTML中的三个下拉菜单添加一些额外选项。第一个和第二个是每个订单的,第三个是每天的。
<select id="extras_1">
<option>10欧元/订单</option>
<option>20欧元/订单</option>
</select>
<select id="extras_2">
<option>20欧元/订单</option>
<option>30欧元/订单</option>
</select>
<select id="extras_3">
<option>10欧元/天</option>
<option>20欧元/天</option>
</select>
我想使用JavaScript将额外选项的价格添加到基本费率中,以便用户可以直接看到最终价格。(最终价格在最后一步中使用PHP计算,所以不用担心安全性问题。)
我是JavaScript的新手。也许有人愿意帮助我吗?谢谢!
英文:
I have the price of an order in an PHP variable, let's say $price. This $price is the sum of a daily $rate * $rental_days. In the next step some extras could be added with three dropdowns in HTML. The first and the second are per order, the third per day.
<select id="extras_1">
<option>10 Euros/Order</option>
<option>20 Euros/Order</option>
</select>
<select id="extras_2">
<option>20 Euros/Order</option>
<option>30 Euros/Order</option>
</select>
<select id="extras_3">
<option>10 Euros/Day</option>
<option>20 Euros/Day</option>
</select>
I would like to add the price of the extras to the basic rate with Javascript, so that the user can see the final price directly. (The final price is calculated with PHP in the last step, so no worries about security.)
I am a total Jacascript newbie. Maybe someone wants to help me? Thank you!
答案1
得分: 1
Sure, here is the translated content:
function calcuate() {
var sum = 0;
var element1 = document.getElementById("extras_1");
sum += Number(element1.value);
var element2 = document.getElementById("extras_2");
sum += Number(element2.value);
var element3 = document.getElementById("extras_3");
sum += Number(element3.value);
console.log(sum);
}
<select id="extras_1">
<option value="10">10欧元/订单</option>
<option value="20">20欧元/订单</option>
</select>
<select id="extras_2">
<option value="10">20欧元/订单</option>
<option value="20">30欧元/订单</option>
</select>
<select id="extras_3">
<option value="10">10欧元/天</option>
<option value="20">20欧元/天</option>
</select>
<button onclick="calcuate()">计算</button>
Please note that I have translated the code while keeping the original IDs and function names intact.
英文:
I too am learning javascript, the code below gets the selected options, extracts number part of the option.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function calcuate() {
var sum = 0;
var element1 = document.getElementById("extras_1");
sum += Number(element1.value);
var element2 = document.getElementById("extras_2");
sum += Number(element2.value);
var element3 = document.getElementById("extras_3");
sum += Number(element3.value);
console.log(sum);
}
<!-- language: lang-html -->
<select id="extras_1">
<option value="10">10 Euros/Order</option>
<option value="20">20 Euros/Order</option>
</select>
<select id="extras_2">
<option value="10">20 Euros/Order</option>
<option value="20">30 Euros/Order</option>
</select>
<select id="extras_3">
<option value="10">10 Euros/Day</option>
<option value="20">20 Euros/Day</option>
</select>
<button onclick="calcuate()">calcuate</button>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论