【发布时间】: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