我正在尝试遍历displayResults
表单的select元素,以检查是否为每个下拉列表选择了一个选项。不幸的是,无论是否选择了任何选项,表单都会提交给服务器。
//Validates that form elements are not empty
function validateForm()
{
for(var i=0; i < document.displayResults.elements.length; i++)
{
if(document.displayResults.elements[i].type=="select")
{
var test = document.displayResults.elements[i].value;
if(test = null || test == "")
{
alert("Please select appropriate options");
return false;
}
}
}
return true;
}
答案 0 :(得分:1)
怎么样:
//Validates that form elements are not empty
function validateForm()
{
var displayResults = document.getElementById("displayResults").elements;
for(var i=0; i < displayResults.length; i++)
{
if(displayResults[i].tagName=="SELECT")
{
var test = displayResults[i].value;
if(!test) //This is essentially the same way you did
{
alert("Please select appropriate options");
return false;
}
}
}
return true;
}