Member 13402731 Ответов: 1

Есть ли лучший способ показать и скрыть множество элементов и снять флажки на hide?


У меня есть этот частичный код, и, как вы видите, он включает в себя множество флажков, радио и скрытых классов. Я пытался писать так, как знаю, но когда списки условий становятся длиннее, это усложняется и выглядит плохим способом написания кода. Поэтому мой вопрос заключается в том, есть ли лучший способ показать и скрыть множество классов, а также скрыть четкие чексбоексы и радиоприемники.

Что я уже пробовал:

function showData() {
  if (relativeYes.checked) {
    document.getElementById('relativeConditions').style.display = 'block';

  } else {
    document.getElementById('relativeConditions').style.display = 'none';
  }
  if (relativeNo.checked) {
    document.getElementById('verification').style.display = 'block';
  }
  else {
    document.getElementById('verification').style.display = 'none';
  }

  if (no.checked) {
    document.getElementById('output').innerHTML = 'Nope';
    document.getElementById('gender').style.display = 'none';
  }
  else {
    document.getElementById('output').innerHTML = '';
  }
  if (yes.checked) {
    document.getElementById('profiling').style.display = 'block';
  }
  else {
    document.getElementById('profiling').style.display = 'none';
  }
  if (profilingYes.checked) {
    document.getElementById('output').innerHTML = 'DO';
    document.getElementById('secondPart').style.display = 'none';
  } else {
    document.getElementById('secondPart').style.display = 'block';
  }
  if (profilingNo.checked) {
    document.getElementById('gender').style.display = 'block';
  }
  if (yes.checked && male.checked) {
    document.getElementById('maleDiagnosis').style.display = 'block';
  } else {
    document.getElementById('maleDiagnosis').style.display = 'none';
  }
  if (yes.checked && female.checked) {
    document.getElementById('femaleDiagnosis').style.display = 'block';
  }
  else {
    document.getElementById('femaleDiagnosis').style.display = 'none';
  }
  if (maleMlm.checked === true && maleDiagnosis.style.display == 'block') {
    document.getElementById('output').innerHTML = 'DO';
  }

  else if (malePrt.checked) {
    document.getElementById('output').innerHTML = 'OTHER';

  }
  if (malePn.checked === true && maleMlm.checked === false) {
    document.getElementById('jaw').style.display = 'block'
  }

  else {
    document.getElementById('jaw').style.display = 'none'
  }
  if (maleMlm.checked === false && malePn.checked === true && malePrt.checked === true) {
    document.getElementById('output').innerHTML = '';
  }
  if (jawYes.checked) {
    document.getElementById('output').innerHTML = 'DO';
  }
  else if (jawNo.checked) {
    document.getElementById('output').innerHTML = 'OTHER';
  }

  if ((maleMlm.checked || malePrt.checked || malePn.checked) && maleDiagnosis.style.display == 'none') {
    maleMlm.checked = maleMlm.unchecked;
    malePrt.checked = malePrt.unchecked;
    malePn.checked = malePn.unchecked;
  }
  if ((jawYes.checked || jawNo.checked) && document.getElementById('jaw').style.display == 'none') {
    jawYes.checked = jawYes.unchecked;
    jawNo.checked = jawNo.unchecked;
  }
  if ((profilingNo.checked || profilingYes.checked) && profiling.style.display == 'none') {
    profilingNo.checked = profilingNo.unchecked;
    profilingYes.checked = profilingYes.unchecked;

  }


}

function clearRadios(id) {
  var Radios = document.getElementById(id).getElementsByTagName('input');
  for (var i = 0; i < Radios.length; i++) {
    if (Radios[i].type == 'radio') {
      Radios[i].checked = false;
    }
  }
}



document.getElementById('container').addEventListener('change', showData);

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="css/style.css">
</head>

<body>

  <div id="container" onchange="showData();">

    <div id="relative">

      Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eaque, laboriosam ? 
      Yes<input id="relativeYes" type="radio" name="relative" onchange="clearRadios('none')"> 
      No<input id="relativeNo"  type="radio" name="relative">

    </div>

    <div id="relativeConditions">
      1. Lorem ipsum dolor sit amet consectetur, adipisicing elit. <input type="radio" name="relaiveConditionsCheck"><br> 
      2. Lorem ipsum dolor sit.****<input type="radio" name="relaiveConditionsCheck"><br> 
      3. Lorem ipsum dolor sit amet consectetur.<input type="radio" name="relaiveConditionsCheck"><br> 
      4. Lorem ipsum dolor sit amet consectetur adipisicing.<input type="radio" name="relaiveConditionsCheck">

    </div>

    <div id="none">

      <div id="verification">

        Lorem ipsum dolor sit?<br> 
        Yes <input id="yes" type="radio" name="verification"> 
        No<input id="no" type="radio" name="verification"><br>

      </div>

      <div id="profiling">

          Lorem ipsum dolor sit amet ?
        Yes<input id="profilingYes" name="profiling" type="radio">
        No<input id="profilingNo" name="profiling" type="radio">

      </div>

      <div id="secondPart">

        <div id="gender">

          GEnder:
          Male</label><input id="male" type="radio" name="gender" >
          Female</label> <input id="female" type="radio" name="gender" ><br>

        </div>
        <div id="maleDiagnosis">
            Mlm<input id="maleMlm"  type="checkbox"> 
            Prt <input id="malePrt" type="checkbox"> 
            Pn<input id="malePn" type="checkbox" ><br>
    </div>

    <p id="jaw">

      Lorem ipsum dolor sores?
        Yes<input id="jawYes" type="radio" name="jaw"> 
        No<input id="jawNo" type="radio" name="jaw">

      </p>

  <div id="femaleDiagnosis" >
   Mm<input type="checkbox">
   Or<input type="checkbox"> 
   Pan<input type="checkbox"><br>
  </div>

</div>
  
</div>

<span style="color:red" onkeyup="showData()">Output:</span><span id="output"> </span>
</div>

</body>
<script src="main.js"></script>
</html>


#jaw,
#age,
#profiling,
#secondPart,
#verification,
#maleDiagnosis,
#femaleDiagnosis,
#relativeConditions, 
#gender {
  display: none;
}

1 Ответов

Рейтинг:
4

SrikantSahu

Похоже, бизнес-логика только делает его громоздким.. Однако вы можете использовать тернарный оператор и Jquery, чтобы сделать его точным и красивым..
Я показал здесь троичные операторы..

<pre>function showData() {
        document.getElementById('relativeConditions').style.display = (relativeYes.checked) ? 'block' : 'none';
        document.getElementById('verification').style.display = (relativeNo.checked) ? 'block' : 'none';
        document.getElementById('gender').style.display = (no.checked) ? (setOutPut('Nope'), 'none') : (setOutPut(''), 'block');
        document.getElementById('profiling').style.display = (yes.checked) ? 'block' : 'none';
        document.getElementById('secondPart').style.display = (profilingYes.checked) ? (setOutPut('Do'), 'none') : 'block';

        if (profilingNo.checked) {
            document.getElementById('gender').style.display = 'block';
        }
        document.getElementById('maleDiagnosis').style.display = (yes.checked && male.checked) ? 'block' : 'none';
        document.getElementById('femaleDiagnosis').style.display = (yes.checked && female.checked) ? 'block' : 'none';

        if (maleMlm.checked === true && maleDiagnosis.style.display == 'block') {
            setOutPut('Do')
        } else if (malePrt.checked) {
            setOutPut('OTHER')
        }
        document.getElementById('jaw').style.display = (malePn.checked === true && maleMlm.checked === false) ? 'block' : 'none';

        if (maleMlm.checked === false && malePn.checked === true && malePrt.checked === true) {
            setOutPut('');
        }
        document.getElementById('output').innerHTML = (jawYes.checked) ? 'DO' : 'OTHER';
        if ((maleMlm.checked || malePrt.checked || malePn.checked) && maleDiagnosis.style.display == 'none') {
            maleMlm.checked = maleMlm.unchecked;
            malePrt.checked = malePrt.unchecked;
            malePn.checked = malePn.unchecked;
        }
        if ((jawYes.checked || jawNo.checked) && document.getElementById('jaw').style.display == 'none') {
            jawYes.checked = jawYes.unchecked;
            jawNo.checked = jawNo.unchecked;
        }
        if ((profilingNo.checked || profilingYes.checked) && profiling.style.display == 'none') {
            profilingNo.checked = profilingNo.unchecked;
            profilingYes.checked = profilingYes.unchecked;
        }
    }

    function setOutPut(text) {
        document.getElementById('output').innerHTML = text;
    }