尝试将单选按钮放置在单选按钮内。

huangapple go评论67阅读模式
英文:

Trying to put radio buttons inside a radio button

问题

以下是您提供的代码的翻译部分:

$(".tier1-options :radio").on("change", function (e) {
    // 从所有tier2选项中删除已显示的类并清除其中所有表单元素的值
    $('.tier2-options')
        .removeClass('shown')
        .find('input, select').val('')
      
    // 显示相关的tier2选项
    $(this)
        .parents('.tier1-options')
        .find('.tier2-options')
        .addClass('shown')
});

$(".subtier1-options :radio").on("change", function (e) {
    // 从所有subtier2选项中删除已显示的类并清除其中所有表单元素的值
    $('.subtier2-options')
        .removeClass('shown')
        .find('input, select').val('')
      
    // 显示相关的subtier2选项
    $(this)
        .parents('.subtier1-options')
        .find('.subtier2-options')
        .addClass('shown')
});
.tier2-options {
    display: none;
    margin-left: 1rem;
    padding: 1rem;
    background-color: #EEE;
}

.shown{
    display: block;
}

.subtier2-options {
    display: none;
    margin-left: 1rem;
    padding: 1rem;
    background-color: #EEE;
}

这是您提供的JavaScript和CSS代码的翻译部分。希望这对您有所帮助。如果您需要更多的翻译,请告诉我。

英文:

Basically, I'm following this post of mine and the following solution works fine if I have to display radio buttons and the and display divs inside it :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;.tier1-options :radio&quot;).on(&quot;change&quot;, function (e) {
  //remove shown class from all tier2 options and clear values from all form elements inside it
  $(&#39;.tier2-options&#39;)
  	.removeClass(&#39;shown&#39;)
    .find(&#39;input, select&#39;).val(&#39;&#39;)
  
  //show the related tier2 options
  $(this)
  	.parents(&#39;.tier1-options&#39;)
    .find(&#39;.tier2-options&#39;)
    .addClass(&#39;shown&#39;)
});

<!-- language: lang-css -->

.tier2-options {
  display: none;
  margin-left: 1rem;
  padding: 1rem;
  background-color: #EEE;
}

.shown{
  display: block;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;h5&gt;Select one of the checkboxes:&lt;/h5&gt;

&lt;div class=&#39;tier1-options&#39;&gt;
  &lt;label&gt;
    &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;changeLocation&quot;&gt;
    Change Location
  &lt;/label&gt;

  &lt;div class=&quot;tier2-options&quot;&gt;
    &lt;select name=&quot;availableMarkAsFullLocations&quot;&gt;
      &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
      &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
      &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
      &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
      &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
      &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
    &lt;/select&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;tier1-options&quot;&gt;
  &lt;label&gt;
    &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;updateLocation&quot;&gt;
    Update Location
  &lt;/label&gt;

  &lt;div class=&quot;tier2-options&quot;&gt;
    &lt;select name=&quot;availableUpdateLocations&quot;&gt;
      &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
      &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
      &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
      &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
      &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
    &lt;/select&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;div class=&quot;tier1-options&quot;&gt;
  &lt;label&gt;
    &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;retireLocation&quot;&gt;
    Retire
  &lt;/label&gt;

  &lt;div class=&quot;tier2-options&quot;&gt;
    &lt;select name=&quot;availableRetireLocations&quot;&gt;
       &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
       &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
       &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
       &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
       &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
       &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
    &lt;/select&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

But when I'm trying to add multiple radio buttons, this code isn't working for Move Contents option. Basicaly the subtier2-options won't show up when I click on Tier I Move or Tier II Move

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;.tier1-options :radio&quot;).on(&quot;change&quot;, function (e) {
      //remove shown class from all tier2 options and clear values from all form elements inside it
      $(&#39;.tier2-options&#39;)
      	.removeClass(&#39;shown&#39;)
        .find(&#39;input, select&#39;).val(&#39;&#39;)
      
      //show the related tier2 options
      $(this)
      	.parents(&#39;.tier1-options&#39;)
        .find(&#39;.tier2-options&#39;)
        .addClass(&#39;shown&#39;)
    });
    
    $(&quot;.subtier1-options :radio&quot;).on(&quot;change&quot;, function (e) {
      //remove shown class from all tier2 options and clear values from all form elements inside it
      $(&#39;.subtier2-options&#39;)
      	.removeClass(&#39;shown&#39;)
        .find(&#39;input, select&#39;).val(&#39;&#39;)
      
      //show the related tier2 options
      $(this)
      	.parents(&#39;.subtier1-options&#39;)
        .find(&#39;.subtier2-options&#39;)
        .addClass(&#39;shown&#39;)
    });

<!-- language: lang-css -->

    .tier2-options {
      display: none;
      margin-left: 1rem;
      padding: 1rem;
      background-color: #EEE;
    }

    .shown{
      display: block;
    }
    
    .subtier2-options {
      display: none;
      margin-left: 1rem;
      padding: 1rem;
      background-color: #EEE;
    }

<!-- language: lang-html -->

    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

    &lt;h5&gt;Select one of the checkboxes:&lt;/h5&gt;

    &lt;div class=&#39;tier1-options&#39;&gt;
      &lt;label&gt;
        &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;changeLocation&quot;&gt;
        Change Location
      &lt;/label&gt;

      &lt;div class=&quot;tier2-options&quot;&gt;
        &lt;select name=&quot;availableMarkAsFullLocations&quot;&gt;
          &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
          &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
          &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
          &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
          &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
          &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;tier1-options&quot;&gt;
      &lt;label&gt;
        &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;updateLocation&quot;&gt;
        Update Location
      &lt;/label&gt;

      &lt;div class=&quot;tier2-options&quot;&gt;
        &lt;select name=&quot;availableUpdateLocations&quot;&gt;
          &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
          &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
          &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
          &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
          &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;


    &lt;div class=&quot;tier1-options&quot;&gt;
      &lt;label&gt;
        &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;retireLocation&quot;&gt;
        Retire
      &lt;/label&gt;

      &lt;div class=&quot;tier2-options&quot;&gt;
        &lt;select name=&quot;availableRetireLocations&quot;&gt;
           &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
           &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
           &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
           &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
           &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
           &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&#39;tier1-options&#39;&gt;
      &lt;label&gt;
        &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;moveContents&quot;&gt;
        Move Contents
      &lt;/label&gt;

        &lt;div class=&quot;tier2-options&quot;&gt;
             &lt;div class=&#39;subtier1-options&#39;&gt;
                 &lt;label&gt;
                  &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;moveContentsTierI&quot;&gt;
                  Tier I move
                &lt;/label&gt;

                 &lt;div class=&quot;subtier2-options&quot;&gt;
                     &lt;select name=&quot;availableTierILocations&quot;&gt;
                     &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
                     &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
                     &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
                     &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
                     &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
                     &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
                  &lt;/select&gt;
                 
                 &lt;/div&gt;
                
             
             &lt;/div&gt; &lt;!--end of &lt;div class=&#39;subtier1-options&#39;&gt; --&gt;
             
             &lt;div class=&#39;subtier1-options&#39;&gt;
                 &lt;label&gt;
                  &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;moveContentsTierII&quot;&gt;
                  Tier II move
                &lt;/label&gt;

                 &lt;div class=&quot;subtier2-options&quot;&gt;
                     &lt;select name=&quot;availableTierIILocations&quot;&gt;
                     &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
                     &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
                     &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
                     &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
                     &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
                     &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
                  &lt;/select&gt;
                 
                 &lt;/div&gt;
                
             
             &lt;/div&gt; &lt;!--end of &lt;div class=&#39;subtier1-options&#39;&gt; --&gt;
             
             
        &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 0

在CSS文件中使用类时,类的应用顺序与它们的书写顺序一致。因此,shown类位于subtier2-options类之上,它会首先被应用。所以,当应用subtier2-options类时,显示被设置为block,然后再更改为none

您只需要在CSS中更改类的顺序,如下所示,然后它就可以正常工作。

.shown{
  display: block;
}

.tier2-options {
  display: none;
  margin-left: 1rem;
  padding: 1rem;
  background-color: #EEE;
}

.subtier2-options {
  display: none;
  margin-left: 1rem;
  padding: 1rem;
  background-color: #EEE;
}

这将确保shown类在tier2-optionssubtier2-options类之前被应用。

英文:

When you use classes within the css file, the classes are applied in the order that they are written. Since the shown class is above the subtier2-options class, it gets applied first. So, display is set to block and then changed to none, when subtier2-options is applied.

You just have to change the order of the classes within the css, as below, and then it works.

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

$(&quot;.tier1-options :radio&quot;).on(&quot;change&quot;, function (e) {
      //remove shown class from all tier2 options and clear values from all form elements inside it
      $(&#39;.tier2-options&#39;)
      	.removeClass(&#39;shown&#39;)
        .find(&#39;input, select&#39;).val(&#39;&#39;)
      
      //show the related tier2 options
      $(this)
      	.parents(&#39;.tier1-options&#39;)
        .find(&#39;.tier2-options&#39;)
        .addClass(&#39;shown&#39;)
    });
    
    $(&quot;.subtier1-options :radio&quot;).on(&quot;change&quot;, function (e) {
      //remove shown class from all tier2 options and clear values from all form elements inside it
      $(&#39;.subtier2-options&#39;)
      	.removeClass(&#39;shown&#39;)
        .find(&#39;input, select&#39;).val(&#39;&#39;)
      
      //show the related tier2 options
      $(this)
      	.parents(&#39;.subtier1-options&#39;)
        .find(&#39;.subtier2-options&#39;)
        .addClass(&#39;shown&#39;)
    });

<!-- language: lang-css -->

    .tier2-options {
      display: none;
      margin-left: 1rem;
      padding: 1rem;
      background-color: #EEE;
    }
    
    .subtier2-options {
      display: none;
      margin-left: 1rem;
      padding: 1rem;
      background-color: #EEE;
    }

    .shown{
      display: block;
    }

<!-- language: lang-html -->

    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

    &lt;h5&gt;Select one of the checkboxes:&lt;/h5&gt;

    &lt;div class=&#39;tier1-options&#39;&gt;
      &lt;label&gt;
        &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;changeLocation&quot;&gt;
        Change Location
      &lt;/label&gt;

      &lt;div class=&quot;tier2-options&quot;&gt;
        &lt;select name=&quot;availableMarkAsFullLocations&quot;&gt;
          &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
          &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
          &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
          &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
          &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
          &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;tier1-options&quot;&gt;
      &lt;label&gt;
        &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;updateLocation&quot;&gt;
        Update Location
      &lt;/label&gt;

      &lt;div class=&quot;tier2-options&quot;&gt;
        &lt;select name=&quot;availableUpdateLocations&quot;&gt;
          &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
          &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
          &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
          &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
          &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;


    &lt;div class=&quot;tier1-options&quot;&gt;
      &lt;label&gt;
        &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;retireLocation&quot;&gt;
        Retire
      &lt;/label&gt;

      &lt;div class=&quot;tier2-options&quot;&gt;
        &lt;select name=&quot;availableRetireLocations&quot;&gt;
           &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
           &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
           &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
           &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
           &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
           &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&#39;tier1-options&#39;&gt;
      &lt;label&gt;
        &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;moveContents&quot;&gt;
        Move Contents
      &lt;/label&gt;

        &lt;div class=&quot;tier2-options&quot;&gt;
             &lt;div class=&#39;subtier1-options&#39;&gt;
                 &lt;label&gt;
                  &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;moveContentsTierI&quot;&gt;
                  Tier I move
                &lt;/label&gt;

                 &lt;div class=&quot;subtier2-options&quot;&gt;
                     &lt;select name=&quot;availableTierILocations&quot;&gt;
                     &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
                     &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
                     &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
                     &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
                     &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
                     &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
                  &lt;/select&gt;
                 
                 &lt;/div&gt;
                
             
             &lt;/div&gt; &lt;!--end of &lt;div class=&#39;subtier1-options&#39;&gt; --&gt;
             
             &lt;div class=&#39;subtier1-options&#39;&gt;
                 &lt;label&gt;
                  &lt;input name=&quot;useroption&quot; type=&quot;radio&quot; value=&quot;moveContentsTierII&quot;&gt;
                  Tier II move
                &lt;/label&gt;

                 &lt;div class=&quot;subtier2-options&quot;&gt;
                     &lt;select name=&quot;availableTierIILocations&quot;&gt;
                     &lt;option value=&quot;-&quot;&gt;--Please Select--&lt;/option&gt;
                     &lt;option value=&quot;3&quot;&gt;BOX#3&lt;/option&gt;
                     &lt;option value=&quot;6&quot;&gt;FREEZER#1&lt;/option&gt;
                     &lt;option value=&quot;8&quot;&gt;FREEZER#2&lt;/option&gt;
                     &lt;option value=&quot;19&quot;&gt;BOX#9&lt;/option&gt;
                     &lt;option value=&quot;20&quot;&gt;QBUILDING&lt;/option&gt;
                  &lt;/select&gt;
                 
                 &lt;/div&gt;
                
             
             &lt;/div&gt; &lt;!--end of &lt;div class=&#39;subtier1-options&#39;&gt; --&gt;
             
             
        &lt;/div&gt;
    &lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月20日 09:39:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定