2015-03-25 61 views
0

所以基本上,我正在爲健身房會員制作申請表。 基本成本是100美元。如果選擇了雜耍,成本將增加50美元。 另外,如果'BMI'> 25且< = 30,則基本成本將增加50美元。如果'BMI'> 30,則其將增加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> 
+0

除非你的健身房會員只能爲高1米或2米,我建議增加一個'步=「0.01」'你的「高度」領域也是如此。當計算BMI時 – Xufox 2015-03-25 09:49:11

回答

0

你還沒有提到你正面臨的確切問題。但是,我認爲這行代碼應該有一些問題。

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

parseInt()是一個函數,你不能給它賦值。

它應該是這樣的

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

而且price.value有些東西不是一個適當的參考,因爲你要分配document.getElementById("price").value;它。所以,你應該用價格而不是price.value

+0

。總數將變爲10050美元而不是150美元 – 2015-03-25 09:53:46

+0

100 + 50 = 10050 ...如果您沒有正確地轉換變量的話,會發生這種情況 – Andromeda 2015-03-25 09:55:53

+0

如果您不確定代碼內部發生了什麼,請在計算的每一步中使用警報,問題出在哪裏 – Andromeda 2015-03-25 09:58:39

相關問題