javascript代码试图制作小费计算器的问题

时间:2014-08-26 12:24:26

标签: javascript html

作为一种练习(我是初学者),我正在尝试创建一个餐馆小费计算器,根据您所在的国家/地区,他会告诉您应该为小费支付多少钱。

好吧,我的代码不起作用,我不知道为什么...... 当我点击提交时,没有任何反应,我不知道为什么......

如果您要查看代码,并告诉我您是否知道问题是什么,我将非常感激。

代码:

    <form action="">

    <span>Please select your country: </span>
                <select name="country" id="select-country">
                            <option value="Israel">Israel</option>
                            <option value="USA">USA</option>
                </select>
                <br/>

                <span>Please enter the price of the dish: </span>
                        <input type="number" max="999" id="number"></input>
                        </br>

            <input type="submit" value="Submit"></input>
    </form>
 <span> Your Tip: </span>
                     <script language="JavaScript" type="text/javascript">
                                     var list = document.getElementById("select-country");
                                     var country = list.options[list.selectedIndex].text;
                                     var price = document.getElementById("number").value;

                                     if (country == "Israel"){
                                                print 'your tip is: ' price/100*12 ;
                                     }
                 </script>

2 个答案:

答案 0 :(得分:1)

<script>
function calcAmt(amountType){
    var price = document.getElementById("number").value;
    var country=amountType;
        if (country == "Israel"){
            document.getElementById('amt').innerHTML=price/100*12;
        }
}
</script>
<span>Please select your country: </span>
<select name="country" id="select-country" onchange="calcAmt(this.value)">
    <option value="">Select Country</option> 
    <option value="Israel">Israel</option>
    <option value="USA">USA</option>
</select>
<br/>
<span>Please enter the price of the dish: </span>
<input type="number" max="999" id="number"></input>
</br>

<input type="submit" value="Submit"></input>
<span> Your Tip: </span>
<span id="amt"></span>

答案 1 :(得分:0)

在您的提交按钮中添加一个javascript函数调用点击...

<input type="submit" value="Submit" onclick="javascript:GetTip()"></input>

在javascript周围添加函数声明,并使用有效的javascript代码显示它...

<script language="JavaScript" type="text/javascript">
    function GetTip() {
                                     var list = document.getElementById("select-country");
                                     var country = list.options[list.selectedIndex].text;
                                     var price = document.getElementById("number").value;

                                     if (country == "Israel"){
                                                alert( 'your tip is: ' + price/100*12 );
                                     }
    }
                 </script>

我知道javascript中没有“打印”功能。