使用单选按钮通过 jquery 动态显示和隐藏 div
我在页面上有以下 HTML 的多个实例:
<div class="input radio optional">
<label class="collection_radio" for="warranty_selected_environments_attributes_1__destroy_false">
<input checked="checked" class="radio optional" id="warranty_selected_environments_attributes_1__destroy_false" name="warranty[selected_environments_attributes][1][_destroy]" type="radio" value="false" />Yes
</label>
<label class="collection_radio" for="warranty_selected_environments_attributes_1__destroy_true">
<input checked="checked" class="radio optional" id="warranty_selected_environments_attributes_1__destroy_true" name="warranty[selected_environments_attributes][1][_destroy]" type="radio" value="true" />No
</label>
</div>
<div class="input numeric integer required">
<label class="integer required" for="warranty_selected_environments_attributes_1_distance">Distance</label>
<input class="numeric integer required" id="warranty_selected_environments_attributes_1_distance" name="warranty[selected_environments_attributes][1][distance]" required="required" size="50" step="1" type="number" />
</div>
此代码是使用 simple_form gem 生成的,因此我对其没有太多控制权。它也是一种嵌套属性形式,因此系统中的每个“环境”都有上述一组代码。我希望能够根据单选组是真还是假来显示和隐藏文本输入“距离”。
我一直在试图找出一种方法来观看广播组,然后知道页面上要隐藏或显示哪个“距离”框。现阶段我能想到的最好的方法是使用名称值,去掉最后一个 [_destroy] 部分并添加 [distance]。值得注意的是,以上所有内容都包含在列出的每个环境的 div 中。
对于冗长的 HTML 表示歉意:)
I have several instances of the following HTML on a page:
<div class="input radio optional">
<label class="collection_radio" for="warranty_selected_environments_attributes_1__destroy_false">
<input checked="checked" class="radio optional" id="warranty_selected_environments_attributes_1__destroy_false" name="warranty[selected_environments_attributes][1][_destroy]" type="radio" value="false" />Yes
</label>
<label class="collection_radio" for="warranty_selected_environments_attributes_1__destroy_true">
<input checked="checked" class="radio optional" id="warranty_selected_environments_attributes_1__destroy_true" name="warranty[selected_environments_attributes][1][_destroy]" type="radio" value="true" />No
</label>
</div>
<div class="input numeric integer required">
<label class="integer required" for="warranty_selected_environments_attributes_1_distance">Distance</label>
<input class="numeric integer required" id="warranty_selected_environments_attributes_1_distance" name="warranty[selected_environments_attributes][1][distance]" required="required" size="50" step="1" type="number" />
</div>
This code is generated with the simple_form gem so I don't have a lot of control over it. It's also a nested attributes form so there is one of the above sets of code for each 'environment' in the system. I want to be able to show and hide the text input 'distance' depending on whether the radio group is either true or false.
I'm stuck at trying to figure out a way to watch a radio group and then know which 'distance' box on the page to hide or show. The best I can think of at this stage is to use the name value, strip off the last [_destroy] part and add [distance]. It's worth noting that all of the above is wrapped in a div per environment listed.
Apologies for the lengthy HTML :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哇,比我想象的容易。 jquery 中方便的closest() 方法使这变得简单:
Wow, easier than I thought. The handy closest() method in jquery makes this easy: