简体   繁体   中英

Change slider to select box

I am having issues trying to figure out how to change my bmi calculator from a slide to a select box and then passing that input to have it be calculated.

For the feet I have the inches set as the values and then when inches are selected they need to be added on to the feet for a total.

    <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 Fiddle: Working example with the jquery ui slide. http://jsfiddle.net/ypyxrks8/

Anyone help with converting this over would be greatly appreciated.

If all you are looking help for is adding the inches to the feet then add this line of code to your calculate button click:

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

That gives you the total number of inches.

You basically had it. That slider is doing some stuff to get values, but you can get the values with parseInt(). I had to add a conversion factor to get it correct, and you'll need to fill in your discrete values for weight, but here is the code:

[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 + '.');

});

Working version here: https://jsfiddle.net/W3AVE/ypyxrks8/7/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM