1

On selecting option 1 the button must be disabled, enable the button when remaining options are selected.

$(document).ready(function(){
  if($("option[value='1']")){
     $("button").prop('disabled',true);
    }
  else{
    $("button").prop('disabled',false);
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="opt">
  <option value="1">one</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>
<button>button</button>
Satpal
  • 132,252
  • 13
  • 159
  • 168
YSuraj
  • 417
  • 5
  • 17

4 Answers4

0

In your code $("option[value='1']") would return a jQuery object which is truthy always so that if statement doesn't make any sense. Although your code only runs once when the page is loaded.

You need to bind a change event handler to update the button based on the current value.

$(document).ready(function() {
  // bind change event handler
  $('#opt').change(function() {
    // update disabled property
    $("button").prop('disabled', this.value == 1);
    // trigger change event to set initial value
  }).change();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="opt">
  <option value="1">one</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>
<button>button</button>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

$(document).ready(function() {
  $('#opt').change(function() {
    $("button").prop('disabled', $(this).val() == 1);//will disable if current value is equals to 1

  }).change()

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="opt">
  <option value="1">one</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>
<button>button</button>

On change event check value of select then depending on value set the disable of button

guradio
  • 15,524
  • 4
  • 36
  • 57
0

Try this. Remove attribute disabled when you want to show things. Read reference and see the following example.

$(document).ready(function(){
  $("button").prop('disabled',true);
  $('#opt').change(function(){
     if($(this).val()==1){
       $("button").attr('disabled','disabled');
      }
    else{
      $("button").removeAttr('disabled');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="opt">
  <option value="1">one</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>
<button>button</button>
Community
  • 1
  • 1
Anson
  • 489
  • 7
  • 12
0

You can try like this

$(document).ready(function () {
        $("#opt").change(function () {
            if ($(this).val() == "1") {
                $("button").prop('disabled', true);
            }
            else {
                $("button").prop('disabled', false);
            }
        })
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="opt">
  <option value="1">one</option>
  <option value="2">two</option>
  <option value="3">three</option>
</select>
<button disabled >button</button>
Bharat
  • 2,441
  • 3
  • 24
  • 36