是否有任何可用的辅助方法可以根据模型属性的值显示/隐藏指定的 div?
这是我想显示 div 的部分内容,如果是,则包装openid_domain_name
文本字段,否则在编辑记录时隐藏。openid_enabled
true
<%= form_for @application do |f| %>
<%= render "shared/error_messages", target: @application %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description %>
<%= f.text_field :description %>
</p>
<p>
<%= f.check_box :openid_enabled %>
<%= f.label 'openid', 'OpenID' %>
<div id="application_openid_domain" style="display: none;">
<%= f.label :openid_domain_name %>
<%= f.text_field :openid_domain_name %>
</div>
</p>
<p><%= f.submit class: "btn btn-primary" %></p>
<% end %>
这是当用户选中/取消选中复选框时显示/隐藏 div 的 javascript。
<script type="text/javascript">
$(function() {
$(':checkbox').click(function () {
if ($(this).is(':checked'))
$("#application_openid_domain").show();
else
$("#application_openid_domain").hide();
});
});
</script>
编辑 生成的 HTML 源代码:
<form accept-charset="UTF-8" action="/applications" class="new_application" id="new_application" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="Cq2rYF1qkjnsobimMTAUvle9Hi82LwdvghcVWRtC/I0=" /></div>
<p>
<label for="application_name">Name</label>
<input id="application_name" name="application[name]" size="30" type="text" />
</p>
<p>
<label for="application_description">Description</label>
<input id="application_description" name="application[description]" size="30" type="text" />
</p>
<p>
<input name="application[openid_enabled]" type="hidden" value="0" /><input id="application_openid_enabled" name="application[openid_enabled]" type="checkbox" value="1" />
<label for="application_openid">OpenID</label>
<div id="application_openid_domain" style="display: none;">
<label for="application_openid_domain_name">Openid domain name</label>
<input id="application_openid_domain_name" name="application[openid_domain_name]" size="30" type="text" />
</div>
</p>
<p><input class="btn btn-primary" name="commit" type="submit" value="Create Application" /></p>
</form>
<script type="text/javascript">
$(function() {
$(':checkbox').click(function () {
if ($(this).is(':checked'))
$("#application_openid_domain").show();
else
$("#application_openid_domain").hide();
});
});
</script>