Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据所选项目和其他值更改输入框的值_Javascript - Fatal编程技术网

Javascript 根据所选项目和其他值更改输入框的值

Javascript 根据所选项目和其他值更改输入框的值,javascript,Javascript,所以基本上,我正在创建一个健身房会员申请表。 基本成本是100美元。如果选择杂耍,费用将增加50美元。 此外,如果“BMI”大于25和30,基本成本将增加50美元,而不是增加100美元。 这是我的密码。它没有按预期工作。我想要一些帮助。非常感谢 <!DOCTYPE html> <html> <head> <title>UWS application form</title> <link re

所以基本上,我正在创建一个健身房会员申请表。 基本成本是100美元。如果选择杂耍,费用将增加50美元。 此外,如果“BMI”大于25和30,基本成本将增加50美元,而不是增加100美元。 这是我的密码。它没有按预期工作。我想要一些帮助。非常感谢

    <!DOCTYPE html>
    <html>
    <head>
    <title>UWS application form</title>
    <link rel="stylesheet" href="Prac1Task2.css" />
    </head>
    <body>
    <form>
    <fieldset>
    <legend>Fitness Details</legend>
Favourite Activities: <br/>
    <input type="checkbox" name="activity" value="cycling" id="cycling"><label for="cycling">Cycling</label>    
    <input type="checkbox" name="activity" value="50" id="juggling" onchange="update(this);"><label for="juggling">Juggling</label>     
    <br/>
    <br/>
    <label for="height">What is your height &#40;Meters&#41;</label><br/>
    <input type="number" name="height" id="height" class="input" onchange="getBMI()">
    <br/>
    <label for="weight">What is your weight? &#40;kg&#41;</label><br/>
    <input type="number" name="weight" id="weight" class="input" onchange="getBMI()">
    <br/>
    <label for="bmi">BMI:</label><br/>
    <input type="number" name="bmi" id="bmi" class="input" value="" readonly>
    <script>
        function getBMI()
        {
            var height = document.getElementById("height").value;
            var weight = document.getElementById("weight").value;
            document.getElementById("bmi").value = (weight / height) / height;
            var bmi = document.getElementById("bmi");
            var price = document.getElementById("price").value;
            if (bmi.value > 25 && bmi.value <= 30)
            {

                parseInt(price.value) += parseInt(50);
            document.getElementById('price').value =  price.value;
            }
            else if (bmi.value > 30)
            {
                document.getElementById("price").value = document.getElementById("price").value + 100;

            }
    }
    </script>

    </fieldset>
    <br/>
    <fieldset>
    <legend>Application Details</legend>
    Date of Application:<br/>
    <input class="input" id="date" name="date" readonly />
    <script>
    var timetime = new Date();
    document.getElementById("date").value = (timetime.getDate() + "/" + (timetime.getMonth() + 1)
                + "/" + timetime.getFullYear());
    </script>
<br/><br/>
Cost of Application: <br/>
$<input type="number" class="input" id="price" name="price" step="0.01" value="100" readonly />
    <script>
    var total = 100;
    function update(feature) {

        if(feature.checked == true){

            total += parseInt(feature.value);
        document.getElementById('price').value =  total;
        }

        if(feature.checked == false){

            total -= parseInt(feature.value);
            document.getElementById('price').value =  total;
            }
    }
    </script>
    </fieldset>
    <br/>
    <input type="submit" value="Submit" class="button">
    </form>
    </body>
    </html>

你没有提到你所面临的确切问题。然而,我认为这行代码应该有一些问题

 parseInt(price.value) += parseInt(50);
parseInt是一个函数,不能为其赋值

应该是这样的

price.value = parseInt(price.value) + parseInt(50);

此外,price.value不是正确的引用,因为您正在分配document.getElementByIdprice.value;去吧。因此,你应该使用price而不是price.value

除非你的健身房会员只能身高1米或2米,否则我建议在计算BMI时,在“身高”字段中添加一个步长=0.01。总数变为$10050,而不是$150100+50=10050…如果您没有正确键入变量,则会发生这种情况。如果您不确定代码内部发生了什么,请在计算的每个步骤上使用警报,并找出问题的确切位置。price.value=parseIntprice.value+parseInt50;。现在,总输入框变为空白,正如我在回答中提到的,使用price而不是price.value