有条件地设置自举按钮的禁用状态

时间:2016-07-05 06:39:39

标签: ruby-on-rails twitter-bootstrap

我的观点中有以下按钮:

<button type="button" class="btn btn-lg btn-primary">Primary button</button>

如果且仅某个条件为disabled,我想向其添加true属性。我怎么这么优雅呢?

4 个答案:

答案 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>