简体   繁体   中英

Check if value is null

var isempty = 0;
collegeArray = $(form).find(".cname").serializeArray();
for (var i = 0; i < collegeArray.length; i++) {
    alert(collegeArray[i].value);
    if (collegeArray[i].value = "") {
        isempty = isempty + 1;
    }
}

if (isempty == 0) {
    container.innerHTML = "";
    document.getElementById('survey').style.visibility = 'visible';
    generateSurvey(collegeArray);
} else {
    alert("Please fill out all fields!");
}

I'm trying to check if a set of fields, has at least one empty field. If there is empty field and alert should pop, if not it should do something else. I'm serializing the input fields as an array and then looping through it. When I run it, it seems that it's alway thinking that there are no empty fields even if there are some. How do I fix this?

您正在使用= ,但需要==

   if(collegeArray[i].value == "")

= is the assignment operator. You mean to use the comparison operator == as Pointy punctually points out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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