![](/img/trans.png)
[英]How can I perform a mathematical calculation on real numbers stored as strings?
[英]How can I perform calculation in Javascript
我上课有2张桌子:
public class Table1 {
public string Name{set;get;}
public string Price{set;get;}
}
public class Table2 {
public string Name{set;get;}
public string Price{set;get;}
}
我有两个输入字段,一个基于用户从两个表中选择的名称,一个具有表1的名称,另一个具有表2的名称。 jQuery应该基于两个选定的名称添加价格。
$(document).on("click", "#buttonClick", function () {
for (var price in data) {
if () {
}
}
});
问题:如何将table1的名称和价格传递给jquery。 当我单击#buttonClick
它应该计算价格。 例如:
Table1
Name Price
RoomA $22
RoomB $23
Table2
Name Price
BuildingA $33
BuildingB $30
如果我从field1(Table1)中选择RoomA,从field2(Table2)中选择BuildingA,那么我应该得到$ 55的输出。 因此,如何将Table1和Table2的Name和Price传递给jquery。 我正在从数据库获取名称和价格。
好的,对于以下html中所述的两个下拉菜单( select
):
<select id="roomz">
<option value="0">Select a room...</option>
<option value="22">Room A</option>
<option value="23">Room B</option>
</select>
<select id="buildingz">
<option value="0">Select a building...</option>
<option value="22">Building A</option>
<option value="23">Building B</option>
</select>
(每个option
value
应为实际价格),
您可以侦听change
事件,获取每个select
value
,将其求和并将结果放在任何需要的位置(例如,添加到某些<input id="result" type="text" />
。
function calculate() {
//get selected values
var roomPrice = $("#roomz").val();
var buildingPrice = $("#buildingz").val();
//get sum of values
var result = parseFloat(roomPrice) + parseFloat(buildingPrice);
//put sum to result
$("#result").val(result);//or val("$" + result) if you want to display dollar sign
}
$(document).ready(function(){
//listen to 'change' event of several selects
$(document).on("change", "#roomz,#buildingz", function (e) {
calculate();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.