应该只显示包含具有相同类的子项的Javascript单选按钮

问题描述 投票:-1回答:1

我已经被锁定了这个逻辑2周,我只需要那个包含“linha-opcao-resposta”类的“div”,这个类有其他同一类的孩子。

换一种说法。我需要让“div”输入父项的子项包含“div”子项,并在选中时显示相同的父类,并将其他子项隐藏在单选按钮组中

请看下面的示例。

<div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
  <div style="float:left;">
    <label for="7329_2338" title="">27.3 Presença de agentes específicos na citologia oncótica?</label>
    <input type="hidden" title="" id="7329_2338" name="hidden_2338">

  </div>
  <div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
    <div style="float:left;">
      <input type="radio" title="" id="7330_2370" name="radio_2370" indicador="CHECKUP_AGEESPCITONC" value="0">
      <label for="7330_2370" title="">Não</label>

    </div>
  </div>
  <div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
    <div style="float:left;">
      <input type="radio" title="" id="7331_2370" name="radio_2370" indicador="CHECKUP_AGEESPCITONC" value="1">
      <label for="7331_2370" title="">Sim </label>

    </div>
    <div style="padding-left: 30px; clear: both; " class="linha-opcao-resposta">
      <div style="float:left;">
        <label for="7331_2371" title="">27.3.1 Agente 1</label>
        <input type="text" title="" id="7331_2371" name="text_2371" indicador="CHECKUP_AGEESPCITONC_AGENTE_1" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">

      </div>
    </div>
    <div style="padding-left: 30px; clear: both; " class="linha-opcao-resposta">
      <div style="float:left;">
        <label for="7332_2371" title="">27.3.2 Agente 2</label>
        <input type="text" title="" id="7332_2371" name="text_2371" indicador="CHECKUP_AGEESPCITONC_AGENTE_2" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">

      </div>
    </div>
    <div style="padding-left: 30px; clear: both; " class="linha-opcao-resposta">
      <div style="float:left;">
        <label for="7333_2371" title="">27.3.3 Agente 3</label>
        <input type="text" title="" id="7333_2371" name="text_2371" indicador="CHECKUP_AGEESPCITONC_AGENTE_3" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">

      </div>
    </div>
  </div>
  <div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
    <div style="float:left;">
      <input type="radio" title="" id="7334_2370" name="radio_2370" indicador="CHECKUP_AGEESPCITONC" value="2">
      <label for="7334_2370" title="">Exame não realizado</label>

    </div>
  </div>
  <div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
    <div style="float:left;">
      <input type="radio" title="" id="7405_2370" name="radio_2370" indicador="CHECKUP_AGEESPCITONC" value="3">
      <label for="7405_2370" title="">Não se aplica</label>

    </div>
  </div>
</div>

应该只显示包含具有相同类别“linha-opcao-resposta”的子项的单选按钮,其他不包含的单选按钮应该使包含子项的那些按钮消失。

我试着这样做,但没有成功。

$('input[type="radio"]').on('change', function () {
    var element = $(this);
    var pais = element.parent().parent();
    var filhos;
    var aux = 0;
    pais.forEach(pai => {
        if (element.val() == aux) {
            filhos = pai.children();
            if (filhos.length > 1) {
                for (var j = 0; j < filhos.length; j++) {
                    filhos[j].style.display = "block";
                }
            }
        }
        else {
            filhos = pai.children();
            for (var k = 0; k < filhos.length; j++) {
                filhos[k].style.display = "none";
            }
        }
        aux++;
    });
javascript html web
1个回答
0
投票

首先将display = 'none'设置为所有单选按钮的父级。然后将display = 'block'设置为匹配条件的单选按钮的父项。尝试以下方式:

var radio = document.querySelectorAll('input[type=radio]');
radio.forEach(function(radEl){
  radEl.parentElement.style.display = 'none';
});

var radShow = document.querySelectorAll('div.linha-opcao-resposta + div.linha-opcao-resposta > div > input[type=radio]');
radShow.forEach(function(radEl){
  radEl.parentElement.style.display = 'block';
});
<div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
  <div style="float:left;">
      <label for="7329_2338" title="">27.3 Presença de agentes específicos na citologia oncótica?</label>
      <input type="hidden" title="" id="7329_2338" name="hidden_2338">

  </div>
  <div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
      <div style="float:left;">
          <input type="radio" title="" id="7330_2370" name="radio_2370" indicador="CHECKUP_AGEESPCITONC" value="0">
          <label for="7330_2370" title="">Não</label>

      </div>
  </div>
  <div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
      <div style="float:left;">
          <input type="radio" title="" id="7331_2370" name="radio_2370" indicador="CHECKUP_AGEESPCITONC" value="1">
          <label for="7331_2370" title="">Sim </label>

      </div>
      <div style="padding-left: 30px; clear: both; " class="linha-opcao-resposta">
          <div style="float:left;">
              <label for="7331_2371" title="">27.3.1 Agente 1</label>
              <input type="text" title="" id="7331_2371" name="text_2371" indicador="CHECKUP_AGEESPCITONC_AGENTE_1" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">

          </div>
      </div>
      <div style="padding-left: 30px; clear: both; " class="linha-opcao-resposta">
          <div style="float:left;">
              <label for="7332_2371" title="">27.3.2 Agente 2</label>
              <input type="text" title="" id="7332_2371" name="text_2371" indicador="CHECKUP_AGEESPCITONC_AGENTE_2" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">

          </div>
      </div>
      <div style="padding-left: 30px; clear: both; " class="linha-opcao-resposta">
          <div style="float:left;">
              <label for="7333_2371" title="">27.3.3 Agente 3</label>
              <input type="text" title="" id="7333_2371" name="text_2371" indicador="CHECKUP_AGEESPCITONC_AGENTE_3" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">

          </div>
      </div>
  </div>
  <div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
      <div style="float:left;">
          <input type="radio" title="" id="7334_2370" name="radio_2370" indicador="CHECKUP_AGEESPCITONC" value="2">
          <label for="7334_2370" title="">Exame não realizado</label>

      </div>
  </div>
  <div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
      <div style="float:left;">
          <input type="radio" title="" id="7405_2370" name="radio_2370" indicador="CHECKUP_AGEESPCITONC" value="3">
          <label for="7405_2370" title="">Não se aplica</label>

      </div>
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.