我的问题是var total不会计算变量,而是将它们串联在一起,但是当我使用数字而不是getAttribute('cost')时,一切都很好... 有什么办法可以解决这个问题吗?
我有此代码
日期:
<div class="row">
<div class="col-lg-2">
<lable for="supervisor " style="font-size: 16px ; text-align: center" class="form-control rounded" >Supervisor </lable>
</div>
<div class="col-lg-2">
<select name="supervisor" id="userslist" style="font-size: 16px ; text-align: center" class="form-control rounded " onchange="calculateCost()">
<option disabled selected >Please Select Supervisor</option>
<option cost="111" cost2="300">John Doe 1 </option>
</select>
</div>
<div class="col-lg-2">
<label for="superextra" style="font-size: 16px ; text-align: center" class="form-control rounded " >Extra Houres</label>
</div>
<div class="col-lg-2">
<input type="number" name="superextra" id="super" min ="0" onchange="calculateCost()" value="0" style="font-size: 16px ; text-align: center" class="form-control rounded ">
</div>
<div class="col-lg-2">
<label for="supertotal" style="font-size: 16px ; text-align: center" class="form-control rounded ">Total Sum</label>
</div>
<div class="col-lg-2">
<input type="number" name="supertotal" id="totalsum" value="" style="font-size: 16px ; text-align: center" class="form-control rounded ">
</div>
</div>
和脚本
function calculateCost()
{
// check first that a user has been selected and also date has been picked
if(document.getElementById('userslist').value && document.getElementById("dateforgame").value)
{
//Supervisor
let timeZone = new Date().toString().match(/([A-Z]+[\+-][0-9]+)/)[1];
let chosenDate = new Date(document.getElementById("dateforgame").value + timeZone);
var obj = document.getElementById('userslist');
if (chosenDate.getDay() === 6) {
var userobj = document.getElementById('userslist');
var selectedUserCost2 = userobj.options[userobj.selectedIndex].getAttribute('cost2');
var extrah = document.getElementById('super').value;
var extra = 240;
var total = selectedUserCost2 + (extra * extrah);
document.getElementById('totalsum').value = total;
} else {
var userobj = document.getElementById('userslist');
var selectedUserCost = userobj.options[userobj.selectedIndex].getAttribute('cost');
var extrah = document.getElementById('super').value;
var extra = 120;
var total = selectedUserCost + (extra * extrah);
document.getElementById('totalsum').value = total;
}
}
}
答案 0 :(得分:0)
getAttribute()
方法的返回值类型为String
,请参见API doc
您需要使用parseInt()
方法将String
类型转换为Number
才能
执行任何算术运算。