如果在 JavaScript 中未选中任何复选框,则需要显示警报,我当前的方法不起作用
我收到“预期对象”错误 - 该错误指向第一个 If... 从我读到的有关复选框的所有内容(复选框对我来说永远不起作用)以及我读到的有关多个条件的内容,这是对的吗?尽管这不是...
var lucy = window.document.alice
if (lucy.ch1.checked != "true" && lucy.ch2.checked != "true" && lucy.ch3.checked != "true" && lucy.ch4.checked != "true")
{
alert('Atleast one box must be checked');
}
if (lucy.skeletor.value = "no")
{
alert('Default Option is not a valid selection.');
}
I'm getting an error for "Object Expected" - the error points to the first If... from everything I read about checkboxes (checkboxes never work for me) and what I read about multiple conditionals, this is right? Even though it isn't...
var lucy = window.document.alice
if (lucy.ch1.checked != "true" && lucy.ch2.checked != "true" && lucy.ch3.checked != "true" && lucy.ch4.checked != "true")
{
alert('Atleast one box must be checked');
}
if (lucy.skeletor.value = "no")
{
alert('Default Option is not a valid selection.');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要
lucy.ch1.checked != "true"
部分。只需说 if (!lucy.ch1.checked && !lucy.ch2.checked && ...
)。此外,使用if
,而不是If
,javascript 区分大小写。您的代码是失败的保证,因此也许您希望将其重写为以下内容:You don't need the
lucy.ch1.checked != "true"
part. Just say if (!lucy.ch1.checked && !lucy.ch2.checked && ...
). Furthermore useif
, notIf
, javascript is case sensitive. Your code is a guarantee for failure, so maybe you want it to rewrite it to something like:尝试
Try
Javascript 区分大小写 - 您需要使用 if()
更新:这是我验证表单的方法(我不太可能将对象称为 lucy,但在这里您可以):
Javascript is case sensitive - you need to use if()
Update: here is a how I would validate a form (I would not likely call the obejct lucy, but here you go):
这是因为你的 true 周围有“”。在这种情况下, true 不是一个字符串,而是一个布尔值,您可以直接写成 true 。
您可以使用 KooiInc 的答案使其变得更加简单。
It's because you have "" around true. In this case true is not a string, it's a boolean value, and you can just write true in stead.
You can make it even more simple by useing KooiInc's answer.