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

拈花ヽ惹草 提交于 2019-12-11 17:50:23

问题


I wanted to make Sweetalert to work only if all the fields are filled. 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.

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'

    }
}

回答1:


You need to get the value of your form inputs. Currently, your name, message and email are all Elements and thus none of them equal "". So your if statement really looks like:

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.



来源:https://stackoverflow.com/questions/56724615/sweetalert-pop-up-message-with-javascript-when-the-fields-are-filled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!