2014-06-23 51 views
0

在我的Ruby on Rails應用程序我有形式,看起來像這樣:Ruby on Rails的,當用戶不接受條款顯示警報使用jQuery

= simple_form_for @reservation do |f| 
    = f.input :terms, as: :boolean 
    = f.submit t('reservation.next'), class: "btn btn-default pull-right" 

在我的模型我有虛擬屬性方面:

validates :terms, acceptance: true, on: :create, allow_nil: false 
attr_accessor :terms 

現在,當用戶不接受條款的應用程序重定向它與閃存錯誤。 但是現在我想當用戶不接受規定的提交按鈕被禁用。有什麼辦法可以通過JQuery來做到這一點? 在此先感謝。

+1

「接受監管」必須是一個複選框樣品例如,然後啓用/禁用提交複選框點擊按鈕。 –

+0

但是,當我做$('input.btn')。attr('disabled',false)我的提交按鈕消失,但我只想不能點擊它。 –

+0

檢查你正在調用的某個地方隱藏按鈕或更改其CSS。 –

回答

1

像這樣的東西應該做的工作。您需要更改此示例中的jQuery選擇器(#your_reservation_terms_checkbox#your_submit_button),以正確匹配這些元素的實際id

= simple_form_for @reservation do |f| 
    = f.input :terms, as: :boolean 
    = f.submit t('reservation.next'), class: "btn btn-default pull-right" 

:javascript 
    $(document).ready(function(){ 
    $('input#your_reservation_terms_checkbox').on('change', function(){ 
     checked = $(this).is(':checked'); 
     $('#your_submit_button').prop('disabled', !checked); 
     $('.warning-message').remove() 
     if(!checked){ 
     $('#your_submit_button').before('<span class="warning-message">You must accept the terms</span>'); 
     } 
    }); 
    }); 
1

假設「接受監管」的複選框,然後寫一個邏輯啓用和禁用使用jQuery提交按鈕,複選框點擊:

使用獨特idcheckbox,並提交button

<input type="checkbox" class="check" value="accept" id="accept"/>accept regulation 

<input type="submit" value="click me" id="submitBtn" disabled/> 


$(document).ready(function(){ 
    $('#accept').on("change",function(){ 
     var checkboxChecked = $(this).is(':checked'); 
     $('#submitBtn').prop('disabled',!checkboxChecked); 
    }); 
}); 

DEMO

1

這裏是如何做到這一點

<input type="checkbox" id="checkbox_id" /> 
<input type="button" id="button_id" value="Submit" /> 

$('#checkbox_id').click(function() { 
     if ($(this).is(':checked')) { 
      $('#button_id').attr('disabled', 'disabled'); 
     } else { 
      $('#button_id').removeAttr('disabled'); 
     } 
    }); 

DEMO