基于model和Jquery启用或禁用

时间:2016-04-07 16:50:29

标签: jquery ruby-on-rails

我正在尝试用我的形式做两件事之一。

  1. 如果传入新模型实例,则禁用所有字段,然后在检查项目时对用户输入作出反应。

  2. 启用/禁用传入的现有模型中定义的字段。

  3. 所有的rails变量都被正常传递了..我的jquery就好了。

    我的Jquery看起来像这样

    $( 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);
    });
    

    我想要的是禁用shipping_speed == 0字段,这意味着尚未设置商店并启用表单以响应用户输入。否则,我希望表单能够反映数据库中的内容。感谢

1 个答案:

答案 0 :(得分:1)

我相信您需要围绕if条件

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

我也使用.removeProp()来启用输入,但这可能只是一种编码风格选择。

$('.free-amount').prop('disabled', 'true');
$('.free-amount').removeProp('disabled');
相关问题