Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Control Dropdown Select Options Menu With Jquery

I am creating a web site. In this web site there is a form and user has to input details. So , in this form , I have added 2 dropdowns like below. Now I want, when I select number 9 from Adults menu, I want to disable children menu. Otherwise if the number is 8 or less than 8, still user can select from children menu. Then I used length in jquery. But, that not what I want.

How can I do this?

$(document).ready(function() {

  var adults = parseFloat($('#adults').val());
  var children = parseFloat($('#children').val());
  var infants = $('#infants').val();
  var Total = adults + children;
  //$("#errorModal").html(Total);

  $('#adults').change(function() {
    var st = $('#adults option:selected');
    if (st.length > 8) {
      $("#children").prop('disabled', true);
    }
  });

  alert(Total);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<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>

  <a href="#" id="ghsubmitbtn" class="btn btn-success">Search Flight Data</a>

</form>
like image 973
Amithash Avatar asked Mar 23 '26 13:03

Amithash


1 Answers

You just need to change length to

 if (st.val() > 8)

DEMO: https://codepen.io/creativedev/pen/ERpXOa

To enable back when value is different:

if (st.val() > 8)
{
     $("#children").prop('disabled', true);
}else{
     $("#children").prop('disabled', false);
}
like image 174
Bhumi Shah Avatar answered Mar 26 '26 04:03

Bhumi Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!