我的观点中有以下按钮:
<button type="button" class="btn btn-lg btn-primary">Primary button</button>
如果且仅某个条件为disabled
,我想向其添加true
属性。我怎么这么优雅呢?
答案 0 :(得分:7)
您可以使用rails helper button_tag
和布尔检查
<%= button_tag "Primary button", class: 'btn btn-lg btn-primary',
disabled: condition_check_method %>
答案 1 :(得分:2)
你应该这样做两种或更多:
<button type="button" class="your-class btn btn-lg btn-primary">Primary button</button>
<script type="text/javascript">
$(function(){
if(Condition){
$('.your-class').addClass('disabled');
}
});
</script>
OR
In The rails term
<button type="button" class="btn btn-lg btn-primary <%= conditon ? "disabled" : ""%>">Primary button</button>
答案 2 :(得分:1)
@ Ropeney的方式是RoR方式,但这是另一种解决方案:
<% #some condition %>
<% foo == bar ? ( disabled = "disabled") : (disabled = "") %>
<!-- insert "disabled" or "" (empty string) in the markup -->
<button type="button" class="btn btn-lg btn-primary" <%= disabled %> >Primary button</button>
注意:我们只能在HTML5中使用disabled
- Correct value for disabled attribute
答案 3 :(得分:0)
我认为这可能适合你
<button type="button" class="btn btn-lg btn-primary" disabled="<%= @check_condition ? 'disabled' : '' %>">Primary button</button>
或
<button type="button" class="btn btn-lg btn-primary" <%= @check_condition ? 'disabled' : '' %>">Primary button</button>