2015-04-01 60 views
1

我有問題試圖找出如何將我的bmi計算器從幻燈片更改爲選擇框,然後通過該輸入來計算它。更改滑塊以選擇框

對於腳我有英寸設置爲值,然後當選擇英寸時,他們需要被添加到腳上的總數。

<div class="form-group"> 
     <label for="height-ft">Feet</label> 
     <select name="height-ft" id="height-ft" class="form-control"> 
      <option value="12">1'</option> 
      <option value="24">2'</option> 
      <option value="36">3'</option> 
      <option value="48">4'</option> 
      <option value="60">5'</option> 
      <option value="72">6'</option> 
      <option value="84">7'</option> 
     </select> 
    </div> 

    <div class="form-group"> 
     <label for="height-in">Inches</label> 
     <select name="height-in" id="height-in" class="form-control"> 
      <option value="1">1"</option> 
      <option value="2">2"</option> 
      <option value="3">3"</option> 
      <option value="4">4"</option> 
      <option value="5">5"</option> 
      <option value="6">6"</option> 
      <option value="7">7"</option> 
      <option value="8">7"</option> 
      <option value="9">9"</option> 
      <option value="10">7"</option> 
      <option value="11">7"</option> 
     </select> 
    </div> 

JS小提琴:用jQuery UI的滑動 工作示例。 http://jsfiddle.net/ypyxrks8/

任何幫助轉換這一點,將不勝感激。

+0

您可能需要選擇值傳遞給隱藏的輸入字段要處理,但不知道這一點。 – Myoji 2015-04-01 17:20:46

回答

1

你基本上已經擁有了它。該滑塊正在做一些東西來獲取值,但是您可以使用parseInt()獲取值。我不得不添加一個換算係數得到它正確的,你需要在你的離散值來填充重量,但這裏是代碼:

[HTML]

<div class="container"> 
<div class="row"> 
    <div class="col-md-12"> <strong>Height:</strong> 

     <div class="form-group"> 
      <select name="height-ft" id="height-ft" class="form-control"> 
       <option value="12">1'</option> 
       <option value="24">2'</option> 
       <option value="36">3'</option> 
       <option value="48">4'</option> 
       <option value="60">5'</option> 
       <option value="72">6'</option> 
       <option value="84">7'</option> 
      </select> 
     </div> 
     <div class="form-group"> 
      <select name="height-in" id="height-in" class="form-control"> 
       <option value="1">1"</option> 
       <option value="2">2"</option> 
       <option value="3">3"</option> 
       <option value="4">4"</option> 
       <option value="5">5"</option> 
       <option value="6">6"</option> 
       <option value="7">7"</option> 
       <option value="8">7"</option> 
       <option value="9">9"</option> 
       <option value="10">10"</option> 
       <option value="11">11"</option> 
      </select> 
     </div> 
    </div> 
    <div class="col-md-12"> <strong>Weight:</strong> 

     <select name="weight-in" id="weight-in" class="form-control"> 
      <option value="100">100</option> 
      <option value="120">120</option> 
      <option value="140">140</option> 
      <option value="160">160</option> 
      <option value="180">180</option> 
      <option value="200">200</option> 
      <option value="220">220</option> 
      <option value="240">240</option> 
      <option value="260">260</option> 
      <option value="280">280</option> 
      <option value="300">300</option> 
     </select> 
     <br> 
    </div> 
    <div type="button" id="calculatebutton" class="btn btn-primary">Calculate</div> 
    <div id="result"></div> 
</div> 

[CSS]

/* BMI Calculator */ 

$('#calculatebutton').click(function() { 
var weight = parseInt($('#weight-in').val()), 
    heightInches = parseInt($('#height-ft').val()) + parseInt($('#height-in').val()), 
    heightsqaured = (heightInches) * (heightInches), 
    result = ((weight)/(heightsqaured) * 703); 
debugger; 
if (result < 16) { 
    var rating = ('You are severely underweight'); 
} else if (result > 16 && result < 18.5) { 
    var rating = ('You are underweight'); 
} else if (result > 18.5 && result < 25) { 
    var rating = ('You are healthy'); 
} else if (result > 25 && result < 30) { 
    var rating = ('You are overweight'); 
} else if (result > 30 && result < 35) { 
    var rating = ('You are moderately obese'); 
} else if (result > 35 && result < 40) { 
    var rating = ('You are severely obese'); 
} else if (result > 40 && result < 80) { 
    var rating = ('You are very severely obese'); 
} else if (result > 80) { 
    var rating = ('This result seems unlikely, please check that the information you have entered is correct'); 
} 
$('#result').html('Your BMI is ' + result.toFixed(1) + '. ' + rating + '.'); 

});

工作版本瀏覽:https://jsfiddle.net/W3AVE/ypyxrks8/7/

1

如果你正在尋找幫助被添加英寸到腳,然後這行代碼添加到您的計算按鈕點擊:

var total = parseInt($('#height-ft').val()) + parseInt($('#height-in').val()); 

這使你英寸的總數。