问题
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