проверить более одного условия в javascript? - PullRequest
0 голосов
/ 26 января 2020

У меня есть форма для заполнения, я хочу проверить, все ли текстовые поля пусты, если они отображаются, то рядом с пустыми отображаются звездочки, если нет (они полны информации), предупреждают о сохраненной странице. контрольная часть и отображение звездочек работает, спасительная часть не работает для меня.

function txtchk(tbox, asterisk, f, name) { //the f parameter is an empty paragraph
        var bol = Boolean;
        if (bol === true) {
            if (document.getElementById(tbox).value == "") {
                document.getElementById(asterisk).style.display = "block";
                document.getElementById(f).innerHTML = name + " required";
                document.getElementById(f).style.display = "block";                  
                return bol = true;

            }

            else {
                document.getElementById(asterisk).style.display = "none";
                document.getElementById(f).style.display = "none";
                return bol = false;
            }

        }
        else {
            alert ("saved");
        }

    }

    function combiner() {
        txtchk("tbox1", "asterisk1", "f1", "Product Name");
        txtchk("tbox2", "asterisk2", "f2", "Description");
        txtchk("tbox3", "asterisk3", "f3", "Image");
        txtchk("tbox4", "asterisk4", "f4", "Unit Price");
        txtchk("tbox5", "asterisk5", "f5", "ISBN");
        txtchk("tbox6", "asterisk6", "f6", "Author");
        txtchk("tbox7", "asterisk7", "f7", "Ammount in the stock");
        txtchk("tbox8", "asterisk8", "f8", "Color");
        txtchk("ops", "asterisk9", "f9", "Size");
        txtchk("tbox10", "asterisk10", "f10", "Manufacturer");
        txtchk("tbox11", "asterisk11", "f11", "Ammount in the stock");

    }

эта функция вызывается кнопкой (при нажатии)

Ответы [ 2 ]

0 голосов
/ 26 января 2020

Смотри здесь ...

const checkboxes = [...document.querySelectorAll('label')];
document.querySelector('button').addEventListener('click', () => {
   if(!checkboxes.some(i => i.querySelector('input').checked)){
      if(confirm("No checkbox is checked, add '*'?")){
          checkboxes.forEach(i => {
              const star = document.createElement('span');
              star.innerHTML = "*";
              i.appendChild(star);
          })
      }
   } else {
      const selectedCheckboxes = checkboxes.map(i => i.querySelector('input')).filter(i => i.checked).map(i => i.value);
        alert(JSON.stringify(selectedCheckboxes))
        console.log(JSON.stringify(selectedCheckboxes));
      }
});
label { display: block; }
<h3>Select A choice</h3>
<hr />
<label>
  <input type="checkbox" name="choice" value="Choice 1" /> Choice 1 
</label>
<label>
  <input type="checkbox" name="choice" value="Choice 2" /> Choice 2 
</label>
<label>
  <input type="checkbox" name="choice" value="Choice 3" /> Choice 3 
</label>
<hr />
<button>Submit</button>
0 голосов
/ 26 января 2020

В вашем текущем коде у вас есть следующее if условие:

var bol = Boolean;

if (bol === true) {
        //do something
    }
    else {
        //do something else
    }

Проблема в том, что значение bol всегда будет одинаковым (объект), и поэтому условие bol === true всегда будет оцениваться как false.

Чего вы пытались достичь с помощью этого конкретного c if заявления?

...