[Fixed]-Cant disable Button using jquery in Django

1πŸ‘

βœ…

Why are you comparing the text of selected option, just compare its value, this’ll be much simpler.

 $("select").on('change',function(){
   if($(this).val()=="blank")
      $("#button").attr('disabled',true)
   else
      $("#button").attr('disabled',false)
});

But Please make sure that you have disabled button on page load.

There is two way to do this working. First, you can disable button on page load in jquery. Second, on page load get the value of select and check that if it’s value is blank then disable the button.

πŸ‘€Poonam

0πŸ‘

Your options text content is actually -, notice the two spaces infront of the dash. The jQuery is reading those spaces and not counting - the same as -. Remove those preceding spaces and it should now work.

$("select").on('change', function() {
  if ($(this).find('option:selected').text() == "-")
    $("#button").attr('disabled', true)
  else
    $("#button").attr('disabled', false)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="{% url 'select_controller' id=i.ID  %}" method="post">
  <td>
    <select class="form-control" name="controller" id="select_controller">
      <option value="test">test</option>
      <option value='blank'>-</option>
      <option value="testing">testing</option>
    </select>
  </td>
  <td>
    <button id="button" class="btn btn-primary" type="submit" value="Submit">Send</button>
  </td>
</form>
πŸ‘€Wowsk

0πŸ‘

We may also use:

$('#button').prop('disabled',true);

Some explanation on .prop() versus .attr() usage here.

Leave a comment