如果在 JavaScript 中未选中任何复选框,则需要显示警报,我当前的方法不起作用

发布于 2024-11-04 20:00:30 字数 444 浏览 0 评论 0原文

我收到“预期对象”错误 - 该错误指向第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

神经暖 2024-11-11 20:00:30

您不需要 lucy.ch1.checked != "true" 部分。只需说 if (!lucy.ch1.checked && !lucy.ch2.checked && ...)。此外,使用 if,而不是 If,javascript 区分大小写。您的代码是失败的保证,因此也许您希望将其重写为以下内容:

var lucy = document.alice; //a form of some kind?
if (!lucy.ch1.checked && !lucy.ch2.checked 
    && !lucy.ch3.checked && !lucy.ch4.checked)
{
  alert('At least one box must be checked');
}

if (lucy.skeletor.value === "no")
{
  alert('Default Option is not a valid selection.');
}

You don't need the lucy.ch1.checked != "true" part. Just say if (!lucy.ch1.checked && !lucy.ch2.checked && ...). Furthermore use if, not If, javascript is case sensitive. Your code is a guarantee for failure, so maybe you want it to rewrite it to something like:

var lucy = document.alice; //a form of some kind?
if (!lucy.ch1.checked && !lucy.ch2.checked 
    && !lucy.ch3.checked && !lucy.ch4.checked)
{
  alert('At least one box must be checked');
}

if (lucy.skeletor.value === "no")
{
  alert('Default Option is not a valid selection.');
}
两人的回忆 2024-11-11 20:00:30

尝试

if (!(lucy.ch1.checked || lucy.ch2.checked || lucy.ch3.checked))

Try

if (!(lucy.ch1.checked || lucy.ch2.checked || lucy.ch3.checked))
青朷 2024-11-11 20:00:30

Javascript 区分大小写 - 您需要使用 if()

更新:这是我验证表单的方法(我不太可能将对象称为 lucy,但在这里您可以):

function validate(lucy) {
  if (!lucy.ch1.checked && !lucy.ch2.checked && !lucy.ch3.checked && !lucy.ch4.checked) {
    alert('At least one box must be checked');
    return false;
  }
  if (lucy.skeletor.value = "no") {
    alert('Default Option is not a valid selection.');
    return false;
  }
  return true;
}

<form onsubmit="return validate(this)">

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):

function validate(lucy) {
  if (!lucy.ch1.checked && !lucy.ch2.checked && !lucy.ch3.checked && !lucy.ch4.checked) {
    alert('At least one box must be checked');
    return false;
  }
  if (lucy.skeletor.value = "no") {
    alert('Default Option is not a valid selection.');
    return false;
  }
  return true;
}

<form onsubmit="return validate(this)">
短叹 2024-11-11 20:00:30

这是因为你的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文