【问题标题】:Check more than one conditions in javascript?在javascript中检查多个条件?
【发布时间】:2020-05-12 03:46:44
【问题描述】:

我有一个表格要填写,我想检查所有文本框是否为空,如果它们是空的,则在空的旁边显示星号,如果不是(它们充满信息)提醒页面已保存。

检查部分和显示星号正在工作,保存部分对我不起作用。

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", "Amount in the stock");
        txtchk("tbox8", "asterisk8", "f8", "Color");
        txtchk("ops", "asterisk9", "f9", "Size");
        txtchk("tbox10", "asterisk10", "f10", "Manufacturer");
        txtchk("tbox11", "asterisk11", "f11", "Amount in the stock");
    }

此函数由按钮调用(点击时)

【问题讨论】:

  • var bol = Boolean; if (bol... 这永远是真的。
  • @LucaKiebel if(bol) 将被评估为 true,但 if(bol === true) 解析为 false。刚刚在我的 Chrome 开发工具中尝试过。不过,您的观点仍然正确,此 if 语句将始终解析为相同的值..
  • @AnisR。你是对的,这似乎是问题
  • 那么最佳做法是什么?

标签: javascript if-statement boolean


【解决方案1】:

在您当前的代码中,您有以下if 条件:

var bol = Boolean;

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

问题在于bol 的值将始终相同(一个对象),因此条件bol === true 将始终评估为false

你想通过这个特定的if 声明来达到什么目的?

【讨论】:

  • 检查编译器是否输入第二个 if (因为 tbox .value == "") 它将返回 true 到 bol,现在如果它没有输入 (tbox.value != "") 它会返回bol为false,则继续第一个if的else,并提示页面。
  • 我认为每次我点击按钮时,编译器都会将 bol 的值设置为 true,因为它位于一切之上,这就是我想要的,还是我想要的?
  • 好吧,如果是这样,我相信您应该真正回顾一下return 语句的工作原理。
【解决方案2】:

看这里...

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>

【讨论】:

  • 也许可以切换星星?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 2022-07-11
  • 2021-10-02
相关资源
最近更新 更多