使用单选按钮通过 jquery 动态显示和隐藏 div

发布于 2024-10-28 12:36:21 字数 1471 浏览 12 评论 0原文

我在页面上有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2024-11-04 12:36:22

哇,比我想象的容易。 jquery 中方便的closest() 方法使这变得简单:

$(":radio").change(function () {
    $(this).closest('.section').children('.input.numeric').toggle(this.checked && this.value == 'false');
});

Wow, easier than I thought. The handy closest() method in jquery makes this easy:

$(":radio").change(function () {
    $(this).closest('.section').children('.input.numeric').toggle(this.checked && this.value == 'false');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文