[英]Ruby on Rails, when user not accepts terms show alert with jQuery
在我的Ruby on Rails應用程序中,我的表單看起來像這樣:
= 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
現在,當用戶不接受條款應用程序時,將發生Flash錯誤,將其重定向回。 但是現在我想在用戶不接受法規時禁用提交按鈕。 JQuery有什么辦法做到這一點? 提前致謝。
假定“接受規則”為復選框,然后使用jQuery編寫邏輯以在單擊復選框時啟用和禁用提交按鈕:
為checkbox
使用唯一的id
,然后提交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);
});
});
像這樣的事情應該做的。 在此示例中,您需要更改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>');
}
});
});
這是有關如何執行此操作的示例示例
<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');
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.