为什么此JavaScript函数不显示警报()?

发布于 2025-01-22 08:40:21 字数 1488 浏览 0 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

蓬勃野心 2025-01-29 08:40:21

该代码不起作用有几个原因。

  1. 您尚未在表格上设置事件侦听器,
  2. 您正在检查是否要检查变量 name 电子邮件是空字符串,而不是检查 name.value email.value 是空字符串。 name 电子邮件是HTML元素,要检查其值,您必须检查 .Value
  3. 您没有检查是否 .value 每个为null,
  4. 您必须在表格的提交回调函数中调用 event.preventdefault()。否则表格的提交将导致页面重新加载。

这是修改工作的代码:

function submitNewsletter(event) {
    event.preventDefault();
    var name = document.getElementById("name");
    var email = document.getElementById("email");
    if (name.value === "" || email.value === "" || !name.value || !email.value) {
        alert("Please fill in your details.");
        return false;
    }
}

document.querySelector(".newsletterForm")
    .addEventListener('submit', submitNewsletter);

jsfiddle: htttps://jsfiddle.net/qu2pesw0/1/

There's a few reasons why this code does not work.

  1. You haven't setup your event listeners on your form
  2. You're checking if the variables name and email are empty strings instead of checking name.value and email.value are empty strings. name and email are HTML Elements, to check their value you must check .value
  3. You're not checking if the .value of each are null
  4. You must call event.preventDefault() in your form's submit callback function. Otherwise the form's submit will cause the page to reload.

Here's the code revised to work:

function submitNewsletter(event) {
    event.preventDefault();
    var name = document.getElementById("name");
    var email = document.getElementById("email");
    if (name.value === "" || email.value === "" || !name.value || !email.value) {
        alert("Please fill in your details.");
        return false;
    }
}

document.querySelector(".newsletterForm")
    .addEventListener('submit', submitNewsletter);

JSFiddle: https://jsfiddle.net/qu2pesw0/1/

不寐倦长更 2025-01-29 08:40:21

您在if语句中检查名称,并通过电子邮件发送是否存在的DOM元素。您想检查输入元素的值。

function submitNewsletter() {
    var name = document.getElementById("name");
    var email = document.getElementById("email");
    if (name.value == "" || email.value == "") {
        alert("Please fill in your details.");
        return false;
    }
}
<section class="signup">
    <h1>Keep up to date.</h1>
    <p>Sign up to our newsletter to hear about improvements and additions to Electric Dreams.</p>
    <form class="newsletterForm">
        <label for="name" class="label">Full Name</label>
        <input type="text" id="name" name="name" class="input" placeholder="Full Name"> <br>
        <label for="email" class="label">Email Address</label>
        <input type="email" id="email" name="email" class="inout" placeholder="Email Address"> <br>
        <button type="submit" class="btn" onclick="submitNewsletter()">Submit</button>
    </form>
</section>

Beacues you checking in the if statement the name, and email the DOM element existing or not. You would like to check the value of the input elements.

function submitNewsletter() {
    var name = document.getElementById("name");
    var email = document.getElementById("email");
    if (name.value == "" || email.value == "") {
        alert("Please fill in your details.");
        return false;
    }
}
<section class="signup">
    <h1>Keep up to date.</h1>
    <p>Sign up to our newsletter to hear about improvements and additions to Electric Dreams.</p>
    <form class="newsletterForm">
        <label for="name" class="label">Full Name</label>
        <input type="text" id="name" name="name" class="input" placeholder="Full Name"> <br>
        <label for="email" class="label">Email Address</label>
        <input type="email" id="email" name="email" class="inout" placeholder="Email Address"> <br>
        <button type="submit" class="btn" onclick="submitNewsletter()">Submit</button>
    </form>
</section>

云淡风轻 2025-01-29 08:40:21

我希望你一切都好。

这就是您首先要获得价值输入的方式

function submitNewsletter() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
if (name == "" || email == "") {
    alert("Please fill in your details.");
    return false;
}

I hope you are well.

This is how you should first get value inputs

function submitNewsletter() {
var name = document.getElementById("name").value;
var email = document.getElementById("email").value;
if (name == "" || email == "") {
    alert("Please fill in your details.");
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文