部分Javascript无法在Chrome中工作
我在Chrome中使用下面的脚本时遇到问题。 (它在Firefox和IE中都能很好地工作。)除了calcTotal函数外,一切都可以工作,但我无法弄清楚原因。 **我添加了我用来获取变量的表格,如果这有助于澄清事情。 **部分Javascript无法在Chrome中工作
<script type="text/javascript"> function calcWages(){
document.getElementById('wagestotal').innerHTML = '';
var hours = new Number(document.getElementById('hours').value);
var rate = new Number(document.getElementById('rate').value);
document.getElementById('wagestotal').innerHTML = ((hours * rate).toFixed(2));
document.getElementById('wagestotal').value = ((hours * rate).toFixed(2));
}
function calcMilage(){
document.getElementById('milagetotal').innerHTML = '';
var miles = new Number(document.getElementById('miles').value);
document.getElementById('milagetotal').innerHTML = ((miles * .535).toFixed(2));
document.getElementById('milagetotal').value = ((miles * .535).toFixed(2));
}
function calcTotal(){
document.getElementById('total').innerHTML = '';
var wages = new Number(document.getElementById('wagestotal').innerHTML);
var milage = new Number(document.getElementById('milagetotal').innerHTML);
var travel = new Number(document.getElementById('travel').value);
var lodging = new Number(document.getElementById('lodging').value);
var food = new Number(document.getElementById('food').value);
var office = new Number(document.getElementById('office').value);
var other = new Number(document.getElementById('other').value);
document.getElementById('total').value = ((wages + milage + travel + lodging + food + office + other).toFixed(2));
}
</script>
Items for Reimbursement:<br/>
<Table>
<tr>
<td>Wages:</td>
<td>Hours:(8 a day Max)<input type="text" name="hours" id="hours" size="4" maxlength="3" onchange="calcWages(); calcTotal()"></td>
<td>Rate:<input type="text" name="rate" id="rate" size="3" maxlength="5" onchange="calcWages(); calcTotal()"></td>
<td>=</td>
<td><input readonly name="wagestotal" id="wagestotal" size="6" maxlength="7"/></td>
</tr>
<tr>
<td>Mileage:</td>
<td>Miles<input type="text" name="miles" id="miles" size="4" maxlength="4" onchange="calcMilage(); calcTotal()"></td>
<td>IRS rate ($0.535)</td>
<td>=</td>
<td><input readonly name="milagetotal" id="milagetotal" size="6" maxlength="7"/></td>
</tr>
<tr>
<td>Travel:</td>
<td colspan="2"><input type="text" name="travelitem" id="travelitem" size="36"></td>
<td>=</td>
<td><input type="text" name="travel" id="travel" size="6" maxlength="7" onchange="calcTotal()"> </td>
</tr>
<tr>
<td>Lodging:</td>
<td colspan="2"><input type="text" name="lodgingitem" id="lodgingitem" size="36"> </td>
<td>=</td>
<td><input type="text" name="lodging" id="lodging" size="6" maxlength="7" onchange="calcTotal()"></td>
</tr>
<tr>
<td>Food:</td>
<td colspan="2"><input type="text" name="fooditem" id="fooditem" size="36"> </td>
<td>=</td>
<td><input type="text" name="food" id="food" size="6" maxlength="7" onchange="calcTotal()"></td>
<tr>
<td>Office Supplies:</td>
<td colspan="2"><input type="text" name="officeitem" id="officeitem" size="36"> </td>
<td>=</td>
<td><input type="text" name="office" id="office" size="6" maxlength="7" onchange="calcTotal()"></td>
</tr>
<tr>
<td>Other:</td>
<td colspan="2"><input type="text" name="otheritem" id="otheritem" size="36"></td>
<td>=</td>
<td><input type="text" name="other" id="other" size="6" maxlength="7" onchange="calcTotal()"></td>
</tr>
<tr>
<td></td>
<td></td>
<td>Grand Total:</td>
<td>=</td>
<td><input readonly name="total" id="total" size="6" maxlength="9"/></td>
</tr>
</table>
回答:
尝试使用
+document.getElementById('wagestotal').innerHTML
,而不是
new Number(document.getElementById('wagestotal').innerHTML)
或只使用
Number(document.getElementById('wagestotal').innerHTML)
在你值的情况下 :
var wages = Number(document.getElementById('wagestotal').innerHTML); var milage = Number(document.getElementById('milagetotal').innerHTML);
var travel = Number(document.getElementById('travel').value);
var lodging = Number(document.getElementById('lodging').value);
var food = Number(document.getElementById('food').value);
var office = Number(document.getElementById('office').value);
var other = Number(document.getElementById('other').value);
document.getElementById('total').value = new Number(wages + milage + travel + lodging + food + office + other).toFixed(2);
以上是 部分Javascript无法在Chrome中工作 的全部内容, 来源链接: utcz.com/qa/261388.html