Новый Dropbox после проверки флажков - PullRequest
0 голосов
/ 07 мая 2018

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

let hiddenMed = document.querySelector('.questionMed');
let hiddenMedQ = document.querySelector(".hiddenQ");
let initialH2 = hiddenMedQ.innerHTML;

//whenever i call the function, whatever is in the parenthesis is going to be known as element within the function
function selectMeds(element) {
    //when you set the variable to element, it enables you to onclick on the thises instead of when you getElementbyId and it only finds the first one
    let checkBox = element;
    //    let checkBox = document.getElementById("medType");
    //    let text = document.getElementById("text");

    if (checkBox.checked == true) {
        hiddenMed.classList.remove("hidden");
        let medName = checkBox.value;
        hiddenMedQ.innerHTML = initialH2 + medName + "?";



    } else {
                let hiddenMed = document.querySelector('.questionMed');
                hiddenMed.classList.add("hidden");
                hiddenMedQ.innerHTML = initialH2;
    }
}
<div class="container question">
        <h2>What kind of medication were you given?</h2>
        <div class="row col-sm-6 medRow1">
            <div class="checkbox">
                <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Tylenol">Tylenol</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Celebrex">Celebrex</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Oxycodone">Oxycodone</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Oxycotin">Oxycotin</label>
            </div>
        </div>
        <div class="row col-sm-6 medRow2">
            <div class="checkbox">
                <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Dilaudid">Dilaudid</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Tramadol">Tramadol</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Aspirin">Aspirin</label>
            </div>
            <div class="checkbox">
                <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Warfarin/Coumadin">Warfarin/Coumadin</label>
            </div>
        </div>
    </div>
    <!--hidden-->
    <div class="container question questionMed hidden">
        <h2 class="hiddenQ">Did you need a refill of </h2>
        <select class="form-control" id="medAmount">
                <option>Select an Option</option>
                <option>Yes</option>
                <option>No</option>
            </select>
    </div>
    <!--hidden-->

1 Ответ

0 голосов
/ 07 мая 2018

Используйте этот код. Этот код, для каждого отмеченного флажка добавьте новый вопрос с его параметрами и, если снимите флажок, удалите его. Надеюсь, это поможет вам

Element.prototype.remove = function() {
  this.parentElement.removeChild(this);
}
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
  for (var i = this.length - 1; i >= 0; i--) {
    if (this[i] && this[i].parentElement) {
      this[i].parentElement.removeChild(this[i]);
    }
  }
}

function addDropDown(id, title) {
  var newDD = `<div class="container question questionMed" id="div-${id}">
  <h2>Did you need a refill of ${title}</h2>
  <select class="form-control" id="dorpdown-${id}">
                    <option>Select an Option</option>
                    <option>Yes</option>
                    <option>No</option>
                </select>
</div>`;
  var div = document.getElementById('endOfPage');
  div.insertAdjacentHTML('beforeend', newDD);
}

function removeDD(id) {
  document.getElementById(id).remove();
}

//whenever i call the function, whatever is in the parenthesis is going to be known as element within the function
function selectMeds(element) {
  //when you set the variable to element, it enables you to onclick on the thises instead of when you getElementbyId and it only finds the first one
  let checkBox = element;
  //    let checkBox = document.getElementById("medType");
  //    let text = document.getElementById("text");
  if (checkBox.checked == true) {
    addDropDown(checkBox.value, checkBox.value);
  } else {
    removeDD('div-' + checkBox.value);
  }
}
<div class="container question">
  <h2>What kind of medication were you given?</h2>
  <div class="row col-sm-6 medRow1">
    <div class="checkbox">
      <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Tylenol">Tylenol</label>
    </div>
    <div class="checkbox">
      <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Celebrex">Celebrex</label>
    </div>
    <div class="checkbox">
      <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Oxycodone">Oxycodone</label>
    </div>
    <div class="checkbox">
      <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Oxycotin">Oxycotin</label>
    </div>
  </div>
  <div class="row col-sm-6 medRow2">
    <div class="checkbox">
      <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Dilaudid">Dilaudid</label>
    </div>
    <div class="checkbox">
      <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Tramadol">Tramadol</label>
    </div>
    <div class="checkbox">
      <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Aspirin">Aspirin</label>
    </div>
    <div class="checkbox">
      <label><input type="checkbox" class="medType" onclick="selectMeds(this)" value="Warfarin/Coumadin">Warfarin/Coumadin</label>
    </div>
  </div>
</div>
<div id="endOfPage"></div>
...