Javascript程序,如果语句有困难

时间:2013-12-13 02:52:31

标签: javascript if-statement

此代码不起作用,至少不完整。我正在尝试运行它以便选择一个项目然后在选择菜单上选择存款金额,直到适当的金额等于或超过,导致变化,但我似乎无法使我的if语句正常工作。如果有人发现问题,我将非常感谢您的帮助。

http://jsfiddle.net/YgX4z/

<script type="text/javascript">

var select;

function changedepositedInput(objDropDown) {
//console.log(parseFloat(objDropDown));
var objdeposited = document.getElementById("deposited");
objdeposited.value=parseFloat(objdeposited.value||'0');
var total=parseInt(objdeposited.value||'0');
objdeposited.value = parseFloat(objDropDown.value||'0')+total;

var cost=parseInt('0');
var water = document.getElementById("water");
var soda = document.getElementById("soda");
var coffee = document.getElementById("coffee");
var beer = document.getElementById("beer");

    if (water.checked) {cost=parseInt('75');}
    if (soda.checked) {cost=parseInt('150');}
    if (coffee.checked) {cost=parseInt('100');}
    if (beer.checked) {cost=parseInt('200');}

if (total>cost) {
    var change=document.getElementById("change");
    change.value=total-cost;
    }
    else {
            var change=document.geElementById("change");
            change.value="0";
    }
if (total>=cost) {
    var objdelivered=document.getElementById("delivered");
    objdelivered.value="Yes";
    }
    else {
            var objdelivered=document.getElementById("delivered");
     objdelivered.value="No";
    }
}




 </script>


<h1>Vending Machine Project</h1>
<form name="vendingmachine" action=" ">
Choose Bevrage<br>
    <input name="item" type="radio" id="water" checked="checked">Water 75 cents<br>
    <input name="item" type="radio" id="soda">Soda $1.50<br>
    <input name="item" type="radio" id="coffee">Coffee $1.00<br>
    <input name="item" type="radio" id="beer">Beer $2.00<br>
    <p>
<label>Deposit Money:
    <select name="money" id="money" onchange="changedepositedInput(this)">
        <option value="0">Choose Amount</option>
        <option value="10">10 cents</option>
        <option value="25">25 cents</option>
        <option value="50">50 cents</option>
        <option value="75">75 cents</option>
        <option value="100">$1.00</option>
    </select>
</label>
    </p>
    <p>Total Deposited:<input name="deposited" id="deposited" type="text" readonly="TRUE" value=""></p>
    <p>Change Returned:<input name="change" id="change" type="text" readonly="TRUE" value=" "></p>
    <p>Bevrage Delivered:<input name="delivered" id="delivered" type="text" readonly="TRUE" value=" "></p>
    <p><input type="reset" value="Start Over"></p>

1 个答案:

答案 0 :(得分:4)

你这里有一个错字:

var change=document.geElementById("change");

应该是

var change=document.getElementById("change");

如果您在小提琴中进行测试,请不要使用function changedepositedInput(),因为它无法正确评估。而是像这样定义你的函数:

changedepositedInput=function(){
};

(注意最后的分号)

<强>提示

您可以使用现代浏览器的错误/ javascript控制台(或通常称为开发人员工具)进行调试。

如果您打开该控制台并运行原始小提琴,则会出现此错误:

[Error] ReferenceError: Can't find variable: changedepositedInput
onchange (_display, line 75)

一旦更改了函数定义(如我之前提到的那样),实际问题就出现了:

[Error] TypeError: 'undefined' is not a function (evaluating 'document.geElementById("change")')
changedepositedInput (_display, line 46)
onchange (_display, line 74)

你指的是错字。

See a working fiddle here