简体   繁体   English

填写字段时,使用JavaScript的SweetAlert弹出消息

[英]SweetAlert pop-up message with JavaScript when the fields are filled

I wanted to make Sweetalert to work only if all the fields are filled. 我想让Sweetalert只在填满所有fields才能工作。 I tried more versions of code, I found answers here on the forum but nothing helped. 我尝试了更多版本的代码,我在论坛上找到了答案,但没有任何帮助。 Now if I do not fill all of the fields and I click on the submit I get notification message because of HTML, but the pop-up appears too. 现在,如果我没有填写所有字段,并且因为HTML而点击submit我收到通知消息,但弹出窗口也会出现。

As you can see here, altough I got the pop-up, I did not fill the last input field. 正如你在这里看到的,尽管我得到了弹出窗口,但我没有填写最后一个输入字段。

function Sweetalert1(){
    var name = document.getElementById("name");
    var message = document.getElementById("message");
    var email = document.getElementById("email");

    if ((name != "" && message != "" && email != "") {
        Swal.fire(
                'Köszönjük!',
                'Megkaptuk a leveledet és hamarosan válaszolunk!',
                'success'

    }
}

You need to get the value of your form inputs. 您需要获取表单输入的值。 Currently, your name , message and email are all Element s and thus none of them equal "" . 目前,您的namemessageemail都是Element ,因此它们都不等于"" So your if statement really looks like: 所以你的if语句真的如下:

if (true && true && true) {
  // code..
}

Instead, you can get the values from your input elements: 相反,您可以从输入元素中获取值:

var name = document.getElementById("name").value;
var message = document.getElementById("message").value;
var email = document.getElementById("email").value;

This way you are getting the text inputted into the input fields, not the elements themselves. 这样您就可以将输入的文本输入到输入字段中,而不是元素本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM