创建游轮票价计算器

时间:2014-09-19 20:46:40

标签: javascript html calculator

我是JavaScript新手。我想根据这些信息制作一艘游轮票价计算器。我试过这个但是我对JavaScript知之甚少,所以我很困惑该怎么做。如果有人帮助我找出我应该在剧本上写的内容,那将会非常有帮助。



 <script type="text/javascript">
    var island = document.getRBtnName("island").value;
    var returnfare = document.getElementByName("returnfare").value;
    var class = document.getRBtnName("class").value;
    function myFunction() {
        document.getElementById("inserts")= island * class * returnfare;
    } 
    </script>
&#13;
<form id="buy">
          <label id="radiolabel">
            <input type="radio" name="island" value="49">
            Main Beach - Azkaban Island</label>
          <br>
          <label id="radiolabel">
            <input type="radio" name="island" value="79">
            Main Beach - Amity Island </label>
          <br>
          <label id="radiolabel">
            <input type="radio" name="island" value="109">
            Main Beach - Treasure Island </label>
          <br>
          <label id="radiolabel">
            <input type="radio" name="island" value="89">
            Main Beach - Gilligan’s Island</label>
          <br>
          <label id="radiolabel">
            <input type="radio" name="island" value="59">
            Main Beach - Skull Island</label>
          <br>
          <p>Click here if you will be purchasing a return fare
            <input type="checkbox" name="returnfare" id="returnfare" value="2">
          </p>
          <label>
            <input type="radio" value="2.5" name="class">
            First class </label>
          <br>
          <label>
            <input type="radio" value="1.5" name="class">
            Business class </label>
          <br>
          <label>
            <input type="radio" value="0" name="class">
            Economy class</label>
          <br>
          <br>
          <input type="submit" name="submit" value="Calculate" id="calculate" onClick="myFunction()">
          <input type="reset" name="reset" value="Reset">
        </form>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

我使用jQuery来修复你的代码,我已经改变了javascript和按钮的类型计算:from type =&#34; submit&#34;类型=&#34;按钮&#34;

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
    </head>
    <body>
        <form id="buy">
          <label id="radiolabel">
            <input type="radio" name="island" value="49">
            Main Beach - Azkaban Island</label>
          <br>
          <label id="radiolabel">
            <input type="radio" name="island" value="79">
            Main Beach - Amity Island </label>
          <br>
          <label id="radiolabel">
            <input type="radio" name="island" value="109">
            Main Beach - Treasure Island </label>
          <br>
          <label id="radiolabel">
            <input type="radio" name="island" value="89">
            Main Beach - Gilligan’s Island</label>
          <br>
          <label id="radiolabel">
            <input type="radio" name="island" value="59">
            Main Beach - Skull Island</label>
          <br>
          <p>Click here if you will be purchasing a return fare
            <input type="checkbox" name="returnfare" id="returnfare" value="2">
          </p>
          <label>
            <input type="radio" value="2.5" name="class">
            First class </label>
          <br>
          <label>
            <input type="radio" value="1.5" name="class">
            Business class </label>
          <br>
          <label>
            <input type="radio" value="0" name="class">
            Economy class</label>
          <br>
          <br>
          <input type="button" name="submit" value="Calculate" id="calculate" onClick="myFunction()">
          <input type="reset" name="reset" value="Reset">
        </form>
        <span id="inserts"></span>

        <script type="text/javascript">
            function myFunction() {
                var island = $('input[name=island]:checked', '#buy').val();
                var myClass = $('input[name=class]:checked', '#buy').val();
                var returnfare = 1;
                if ($("#returnfare").prop('checked')) {
                    returnfare = 2;
                }
                var total = island * myClass * returnfare;
                $("#inserts").text(total);

            } 
        </script>

    </body>
</html>

答案 1 :(得分:0)

我发现这里的圆形图是JavaScript代码:

<script type="text/javascript">
            function myFunction() {
   				var island = $('input[name=island]:checked', '#buy').val();
                var myClass = $('input[name=class]:checked', '#buy').val();
                var returnfare = 1;
                if ($("#returnfare").prop('checked')) {
                    returnfare = 2;
                }

                var total = island * myClass * returnfare;
				if (total == '122.5', '197.5', '272.5', '222.5', '147.5', '88.5', '133.5', '163.5', '118.5', '73.5') {total = Math.round (total)
				 total = total;
				} 
				 alert("The cost of the cruise is $" + total);

            } 
			window.onload = function () {
    document.getElementById("calculate").onclick = myFunction;
}


        </script>

答案 2 :(得分:-2)

我没时间编写代码给你很抱歉,但你需要做的是给目的地一个特定的id,比如所有目的地的“目的地”相同的id。然后是复选框的ID,以及他们想要乘坐的类的相同ID。

然后制作x y和z并将它们设置为它们所选择的选项的值。 然后创建另一个名为total的var并执行* x +(x * y)+(x * z)并显示提交的总onclick。

我希望我有点帮助。很抱歉再没有提供任何代码。