如果 a 或 b = null 或没有值,那么... 填写最后一个字段会覆盖函数吗?
Posted
技术标签:
【中文标题】如果 a 或 b = null 或没有值,那么... 填写最后一个字段会覆盖函数吗?【英文标题】:If a or b = null or no value then... Filling in last field overrides function? 【发布时间】:2016-01-21 20:45:51 【问题描述】:我有这个代码:
if(document.getElementById('UserType_2').checked)
if(a==null || a=="", b==null || b=="",
c==null || c=="", d==null || d=="",
k==null || k=="", e==null || e=="",
g==null || g=="", h==null || h=="",
j==null || j=="", k==null || k=="")
document.forms["619new"]["next1"].disabled = true;
document.forms["619new"]["next1"].style.background='#FDFDFD';
document.forms["619new"]["next1"].style.color = 'lightgray';
document.forms["619new"]["next1"].style.border = 'none';
document.forms["619new"]["next1"].style.cursor = 'not-allowed';
else
document.forms["619new"]["next1"].disabled = false;
document.forms["619new"]["next1"].style.background='#195dad';
document.forms["619new"]["next1"].style.color = 'lightgray';
document.forms["619new"]["next1"].style.border = 'none';
document.forms["619new"]["next1"].style.cursor = 'pointer';
除非输入字段,否则它意味着禁用按钮。一旦输入按钮变得可点击。
这行得通。但是...如果我决定首先填写最后一个字段,它会覆盖所有其他字段并在其他字段为空时解锁按钮。
我试图找到解决方法,但卡住了。任何人都可以帮忙吗?
【问题讨论】:
我很确定您的if
语法是错误的...而不是逗号,您应该使用 ||
您可能应该标记这是什么语言以及什么 UI 框架(如果该语言有多个)。
嗨,它是 javascript。
||作品。谢谢@adjit
【参考方案1】:
您看到此行为的原因是您在 if 语句中使用了逗号。令我惊讶的是,javascript 可以很好地接受它们,但操作的结果是最后一个操作数的值。 在这里查看这个答案Why does javascript accept commas in if statements?
更新:
将逗号更改为||
,然后尝试更改添加和删除按钮中禁用属性的方式。最后尝试使用!
来否定您的陈述,因为您正在检查的内容没有考虑undefined
的情况。
if(document.getElementById('UserType_2').checked)
if(!a || !b || !c ||
!d || !e || !f || !g ||
!h || !i || !j || !k )
document.forms["619new"].next1.disabled = "disabled";
document.forms["619new"].next1.style.background='#FDFDFD';
document.forms["619new"].next1.style.color = 'lightgray';
document.forms["619new"].next1.style.border = 'none';
document.forms["619new"].next1.style.cursor = 'not-allowed';
else
delete document.forms["619new"].next1.disabled;
document.forms["619new"].next1.style.background='#195dad';
document.forms["619new"].next1.style.color = 'lightgray';
document.forms["619new"].next1.style.border = 'none';
document.forms["619new"].next1.style.cursor = 'pointer';
【讨论】:
【参考方案2】:就像@Adjit 所说的,只需使用 ||而不是逗号。谢谢,我以为我在幸运地再次尝试之前尝试过!
【讨论】:
为什么/如何帮助问答系统? 然后,请编辑您的答案以说明解决问题的原因(以便新用户可以阅读并解决他们的问题),或者将引导您解决问题的答案勾选为“正确” ....通过单击答案左侧箭头下方的绿色“复选标记”来执行此操作以上是关于如果 a 或 b = null 或没有值,那么... 填写最后一个字段会覆盖函数吗?的主要内容,如果未能解决你的问题,请参考以下文章