lundi 5 septembre 2016

disable input on checkbox in rails app

I have a date input dropdown on a form in my rails application.

I'd like there to be a checkbox that- when checked/selected by the user- will (#1) disable the date input and (#2) populate the date input with the current date.

I've looked at previous SO answers to get an idea of how to do this and here's what I tried to accomplish #1:

app/assets/javascripts/application.js

 //= require jquery
 //= require bootstrap
 //= require jquery_ujs
 //= require turbolinks
 //= require_tree .

$(document).on('change', '#availableNow', function(){
    debugger;
    if($(this).prop('checked')){
        $('#fromDatePicker').attr('disabled', 'disabled');
    } else {
        $('#fromDatePicker').removeAttr('disabled');
    }
});

In the form...

app/views/apartments/_form.html.erb

   <div class="field">
      <%= f.label :available_now %>
      <%= f.check_box :available_now, id:"availableNow" %>
    </div>
    <div class="field">
      <%= f.label :to_date %><br>
      <%= f.date_select :to_date, {:include_blank => true, :default => nil, :start_year => Date.today.year, :end_year => Date.today.year + 4, :order => [:month, :day, :year] }, id:"fromDatePicker" %>
    </div>

Any ideas why the disable upon checkbox functionality is not working?

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire