How To Control Input Type Dropdown Select Options Menu With Jquery
I am creating a web site. In this web site , there is a form. I have added 3 dropdown menus in this form like below. Now I want to when someone selects the number 5 from Adults menu , I want to show number 5 in the infants menu. And also I want t keep Adults + Children = 9. Then if this Total is below than 9 , I want to show that extra number in the infants menu. I mean like this ,
Adults + Children = 9
Adults = 5 and infants = 5
But , Adults + Children = 8
Adults = 5 , Children = 3 , it means there is still 1.
Now I want to show that 1 with infants , 1 + 5 = 6.
Here is the Jquery part.
- $(document).ready(function() {
- var adults = parseFloat($('#adults').val());
- var children = parseFloat($('#children').val());
- var infants = $('#infants').val();
- var Total = adults + children;
- //$("#errorModal").html(Total);
- var totalAdultChild = 9;
- $('#adults').change(function() {
- var adultValue = this.value;
- if (this.value > 8) {
- $("#children").prop('disabled', true);
- } else {
- $("#children").prop('disabled', false);
- $('#children option').each(function(index, element) {
- if ((totalAdultChild - adultValue) < this.value)
- $(this).hide();
- else
- $(this).show();
- });
- $('#infants option').each(function(index, element) {
- if (adultValue < this.value)
- $(this).hide();
- else
- $(this).show();
- });
- }
- });
- //alert(Total);
- });
Here is HTML Dropdown View.
- <form class="form-horizontal" method="POST" action="#" enctype="multipart/form-data" id="signupForm">
- <div class="col-md-4 col-sm-12 hero-feature">
- <!-- Start Of The Col Class -->
- Adults :
- <select name="adults" class="form-control" id="adults">
- <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">8</option>
- <option value="9">9</option>
- </select> <br>
- </div>
- <div class="col-md-4 col-sm-12 hero-feature">
- <!-- Start Of The Col Class -->
- Children :
- <select name="children" class="form-control" id="children">
- <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">8</option>
- <option value="9">9</option>
- </select> <br>
- </div>
- <div class="col-md-4 col-sm-12 hero-feature">
- <!-- Start Of The Col Class -->
- Children :
- <select name="infants" class="form-control" id="infants">
- <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">8</option>
- <option value="9">9</option>
- </select> <br>
- </div>
- <a href="#" id="ghsubmitbtn" class="btn btn-success">Search Flight Data</a>
- </form>