如果有人选中 bool 值:someday
,我们如何触发条件?
<%= simple_form_for(@challenge, html: { data: { modal: true } }) do |f| %>
<%= f.check_box :someday %> # If checkmarked show first condition else show second condition
<% if :someday == true %> # How to Write this line?
<%= link_to 'Visit London', create_challenge_path(challenge: {name: 'Visit London', categorization: 'adventure', category: 'goal'}), class: "featured-challenge" %>
<% else %>
<%= button_tag(type: 'submit', class: "btn", id: "challenge-button") do %>
Save
<% end %>
<% end %>
最佳答案
当用户选中复选框时,您希望显示<%= link_to 'Visit London', create_challenge_path(challenge: {name: 'Visit London', categorization: 'adventure', category: 'goal'}), class: "featured-challenge" %>
,否则你想显示 <%= button_tag(type: 'submit', class: "btn", id: "challenge-button") %>
,正确吗?
我假设“保存”按钮始终可见,并且我假设您的复选框的默认状态为“false”。
您可以隐藏.featured-challenge
从屏幕上,如果复选框被选中,则检查 jQuery:
.featured-challenge { display: none; }
JS 中的某个位置(也许是 featured-challenge.js):
$(document).ready(function(){
$('input[type="checkbox"]').click(function(){
$(".featured-challenge").toggle();
});
});
关于javascript - 根据对象属性在 View 中创建条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37646613/