编辑表单时显示/隐藏持久性,其中单选按钮用于显示/隐藏表单元素 [英] Show/Hide persistence when editing form where radio button is used to show/hide forms element
问题描述
此代码可以正常工作,但是必须在Jquery脚本中有更好的方法.
This code is working but there must be a better way to do this in the Jquery script.
这是我的单选按钮
<%= f.radio_button :losstype, "Cash", :id => 'lc' %>Cash
<%= f.radio_button :losstype, "Asset",:id => 'rb' %>Asset
<%= f.radio_button :losstype, "Supplies", :id => 'ab' %>Supplies
这就是我现在隐藏的地方.
And heres my hidden bit right now.
<% if @assetloss.losstype == "Asset" %>
<div id="asset_list" style="display: show;">
<% else %>
<div id="asset_list" style="display: none;">
<% end >
A drop down list to be shown
</div>
还有Jquery部分
$j(document).ready(function(){
$j("input[id='lc']").change(function() {
$j("#asset_list").hide();
} );
$j("input[id='rb']").change(function() {
$j("#asset_list").show("slide");
} );
$j("input[id='ab']").change(function() {
$j("#asset_list").hide();
} );
} );
选中的单选按钮上的呈现标记
Rendered Markup on checked radio button
<input checked="checked" id="rb" name="asset_loss[loss_type]" type="radio" value="Asset"/>
虽然可行,但我确信有更好的方法可以做到这一点,所以我会要求我提供重构帮助.
While it works, I'm sure there's a better way to do this, so I would be asking for refactoring help I guess.
非常感谢
Ed1.我已经意识到这是一个Jquery问题,需要它来检查是否从数据库中检查了"input [id ='rb']",然后$ j(#asset_list").show();无需更改侦听器
Ed1. I have realized this is a Jquery Question and need it to check that if "input[id='rb']" is checked (from db) then $j("#asset_list").show(); without needing a change listener
推荐答案
首先,我给他们所有相同的名字,将他们分组在一起.
First I would give them all the same name to group them together.
$j("input[name='btnGroupName']").change(function() {
if($('#rb').is(':checked')) {
$j("#asset_list").show("slide");
}else{
$j("#asset_list").hide();
}
});
这篇关于编辑表单时显示/隐藏持久性,其中单选按钮用于显示/隐藏表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!