2016-04-08 10 views
1

:現在のボックスにチェックするときチェックすると、私はこれらのコードを持って無効フォーム

<div class="nested-fields"> 
    <div class="row"> 
    <div class="col-sm-4"> 
     <div class="form-group"> 
     <%= f.input :start_work, as: :date, start_year: Date.today.year - 20, 
              end_year: Date.today.year, ignore_day: true, 
              order: [:month, :year], 
              input_html: { class: 'form-control' }, 
              label: 'Start work period' %> 
     </div> 

     <div class="form-group"> 
     <%= f.input :is_current, label: 'Currently work here', input_html: { class: 'current' } %> 
     </div> 
    </div> 

    <div class="col-sm-4"> 
     <div class="form-group"> 
     <%= f.input :end_work, input_html: {class: 'end_work'}, as: :date, start_year: Date.today.year - 20, 
              end_year: Date.today.year, ignore_day: true, 
              order: [:month, :year], 
              input_html: { class: 'form-control' }, 
              label: 'End work period' %> 
     </div> 
    </div> 
    </div> 
</div> 

<script> 
    $(document).ready(function() { 
    // experience current work 
    $(".current").each(function() { 
     $(this).click(function() { 
     var isCurrent = $(this).prop("checked"); 
     if (isCurrent == true) { 
      $(this).closest('.form-group').next('.form-group').find('.end_work').prop("disabled", "disabled"); 
     } 
     else { 
      $(this).closest('.form-group').next('.form-group').find('.end_work').prop("disabled", false); 
     } 
     }); 
    }); 
    }); 
</script> 

はしかし、私は私のend_work入力を無効にすることはできません。私は、Javascriptでターゲットクラスや何かを逃したと思う。どんな助けも素晴らしいでしょう!

答えて

0

$(this).closest('.form-group').next('.form-group')あなたはそう思わないと思います。 div.form-groupはDOM内の兄弟ではないので、私が間違っていなければ、.next('.form-group')は何も見つけることができません。

Try $(this).closest('.col-sm-4').next('.col-sm-4').find('.end_work')


ところで、このコードは冗長である:既にセレクタにマッチするすべての要素へのクリックハンドラをattachs

$(".current").each(function() { 
    $(this).click(function() { 
    ... 

... .clickので。 1つの行で同じことを達成できます。

$(".current").click(function() { 
    ... 
関連する問題