Javascript 看起来像这样:
<script type="text/javascript">
$(document).ready(function(){
var totalOwed = <?php echo $totalTally; ?>;
$("#cash").keyup(function() { doThis(); });
$("#card").keyup(function() { doThis(); });
var paidSoFar = 0;
function doThis() {
paidSoFar = parseInt($("#cash").val()) + parseInt($("#card").val());
alert(paidSoFar);
if (paidSoFar >= totalOwed) {
endCalc = paidSoFar - totalOwed;
$("#endCalc").html("Change: " + endCalc);
}
else {
remaining = totalOwed - paidSoFar;
$("#endCalc").html("Remaining: " + endCalc);
}
}
});
</script>
HTML如下:
Total Owed: <?php echo $totalTally; ?><br />
Cash: <input type="text" id="cash" /><br />
Card: <input type="text" id="card" /><br />
<div id="endCalc"></div>
但是当paidSoFar 变量尝试读取文本输入的值时,它最终会变成 NaN。
日期
现在,我的 Javascript 看起来像这样:
<script type="text/javascript">
$(document).ready(function(){
var totalOwed = <?php echo $totalTally; ?>;
$("#cash").keyup(function() { doThis(); });
$("#card").keyup(function() { doThis(); });
var paidSoFar = 0;
function doThis() {
paidSoFar = parseInt($("#cash").val(), 10) + parseInt($("#card").val(), 10);
alert(paidSoFar);
if (paidSoFar >= totalOwed) {
endCalc = paidSoFar - totalOwed;
$("#endCalc").html("Change: " + endCalc);
}
else {
remaining = totalOwed - paidSoFar;
$("#endCalc").html("Remaining: " + endCalc);
}
}
});
</script>
HTML如下:
Total Owed: <?php echo $totalTally; ?><br />
Cash: <input type="text" id="cash" /><br />
Card: <input type="text" id="card" /><br />
<div id="endCalc"></div>
但是paidSoFar
变量最终会NaN
在它尝试读取文本输入的值时结束。