ToddT
ToddT

Reputation: 3258

Enable or disable based on model and Jquery

I am trying to do one of two things with my form.

  1. Disable all fields if a new model instance is passed in and then react to user input as items are checked.

  2. Enable/disable the fields as defined in an existing model that is passed in.

All the rails variables are being passed in fine.. Its my jquery thats jacked.

My Jquery looks like this

$( document ).ready(function() {
var shipping = function() {
  if($(".<%= type %>-check").is(':checked')) {
    $('.<%= type %>').prop( "disabled", false );}
  else{$('.<%= type %>').prop( "disabled", true );}
}

var free = function() {
  if($('.free-check').is(':checked')) {
    $('.free-amount').prop( "disabled", false );}
  else{$('.free-amount').prop( "disabled", true );}
}

 if <%= shipping_shop.shipping_speed %> == 0 {
  $('.free-amount').prop( "disabled", true );
  $('.<%= type %>').prop( "disabled", true );  
 }
 else {
   shipping();
   free();
 }

$('.<%= type %>-check').on('change', shipping);
$('.free-check').on('change', free);
});

What I want is to disable the fields if shipping_speed == 0 which means the shop hasn't been setup yet and enable the form to react to user input. Otherwise I want the form to reflect what's in the DB. Thanks

Upvotes: 0

Views: 131

Answers (1)

Ryan Dantzler
Ryan Dantzler

Reputation: 1144

I believe you need parentheses around your if condition

 if (<%= shipping_shop.shipping_speed %> == 0) {
  $('.free-amount').prop( "disabled", true );
  $('.<%= type %>').prop( "disabled", true );  
 }
 else {
   shipping();
   free();
 }

Also I use .removeProp() to enable the input but that may just be a coding style choice.

$('.free-amount').prop('disabled', 'true');
$('.free-amount').removeProp('disabled');

Upvotes: 1

Related Questions