[英]How do I find out if a input element's value is blank (null)
I have following code to check if the inputs
with the ids
emailForm
and nameForm
are blank, this however isn't working when I test the form by leaving it blank. 我有以下代码检查ids
emailForm
和nameForm
的inputs
nameForm
为空,但是当我通过将其保留为空白来测试表单时,此方法不起作用。
function setInfo() {
if (document.getElementById("emailForm").value == null ||
document.getElementById("nameForm").value == null) {
alert("Please Fill in all sections");
} else {
email = document.getElementById("emailForm").value;
name = document.getElementById("nameForm").value;
loaded();
}
}
Could someone help me with this, thanks! 有人可以帮我吗,谢谢!
Instead of checking for null
specifically, you should check for falsy values. 您应该检查伪造的值,而不是专门检查null
。 In some cases, the values for empty textboxes will be an empty string. 在某些情况下,空文本框的值将是一个空字符串。
Replace this: 替换为:
if (document.getElementById("emailForm").value == null || document.getElementById("nameForm").value == null) {
with this: 有了这个:
if (!document.getElementById("emailForm").value || !document.getElementById("nameForm").value) {
You shouldn't be checking whether the fields are null
, you should be checking whether they content is an empty string (with .value == ''
). 您不应该检查字段是否为null
,而应该检查它们的内容是否为空字符串( .value == ''
)。
This can be seen working in the following: 可以在以下方法中看到它的工作方式:
function setInfo() { if (document.getElementById("emailForm").value == '' || document.getElementById("nameForm").value == '') { console.log("Please fill in all sections"); } else { email = document.getElementById("emailForm").value; name = document.getElementById("nameForm").value; //loaded(); console.log("All sections filled in"); } } const button = document.getElementById('go'); button.addEventListener('click', function() { setInfo(); });
<input id="emailForm" /> <input id="nameForm" /> <button id="go">Go</button>
Make sure you calling function setInfo()
确保调用函数setInfo()
function setInfo() {
// You can check Value.Length also or
if (document.getElementById("emailForm").value === "" ||
document.getElementById("nameForm").value === "") {
alert("Please Fill in all sections");
} else {
email = document.getElementById("emailForm").value;
name = document.getElementById("nameForm").value;
loaded();
}
}
Try below solution: 请尝试以下解决方案:
function setInfo() {
var email=document.getElementById("emailForm").value;
var name=document.getElementById("nameForm").value;
if (email=='' || email==null || name=='' || name== null ) { // OR if (!email || !name)
alert("Please Fill in all sections");
return;
} else {
loaded();
}
}
You should check whether the string is empty or not instead of null. 您应该检查字符串是否为空而不是null。 Try using the code below: 尝试使用以下代码:
function setInfo() {
var a=document.getElementById("emailForm").value;
var b=document.getElementById("nameForm").value;
if (a == "" ||
b == "") {
alert("Please Fill in all sections");
} else {
email =
document.getElementById("emailForm").value;
name =
document.getElementById("nameForm").value;
alert("success alert");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.