使用javascript检查表单字段是否为空

时间:2016-11-17 15:13:07

标签: javascript html

我目前有一个工作函数,用于检查所有表单字段是否都不为空。对于表单的一部分,我有一个隐藏的div,如果其中一个字段在下拉列表中选择了“是”,则会显示该div。当我将下拉项目选为是时,我希望函数检查这些字段中是否有任何一个是空的,当下拉的答案为否时,我不希望函数为检查隐藏div项的值。我似乎无法找到实现这一目标的方法。这是我到目前为止所尝试的。变量label2包含下拉菜单的值,是/否。

function validateOverage()
{
    var drive2 = document.forms["overage"]["drivers2"].value;
    var prodes2 = document.forms["overage"]["proddes2"].value;
    var sizes2 = document.forms["overage"]["size2"].value;
    var cases2 = document.forms["overage"]["cs2"].value;
    var bottle2 = document.forms["overage"]["btls2"].value;
    var initials2 = document.forms["overage"]["chkitls2"].value;
    var label2 = document.forms["overage"]["ontrucks"].value;
    var routenum2 = document.forms["overage"]["routenum"].value;
    var stopnum2 = document.forms["overage"]["stopnum"].value;
    var custnum2 = document.forms["overage"]["custnum"].value;
    if (drive2 == null || drive2 == "" || prodes2 == null || prodes2 == "" || sizes2 == null || sizes2 == "" || cases2 == null || cases2 == ""||
    bottle2 == null || bottle2 == "" || initials2 == null || initials2 == "" || label2 == null || label2 == "")
    {
        alert("Fill in all Fields for the Overage form");
        return false;
    }
    else if (label2 == "Yes" || routenum2 == null || routenum2 == "" || stopnum2 == null || stopnum2 == "" || custnum2 == null || custnum2 == "")
    {
        alert("Fille in all Fields for the Overage form");
        return false;
    }*/
    else if (cases2 == "0" && bottle2 == "0")
    {
        alert("Both cases and bottles values can not be 0");
        return false;
    }
    else
    {
        alert("Data Added to the Overage form");
    }
}

我身体中有隐藏内容的HTML代码

<td style = "visibility: hidden"><input type = "number" id = "routenum"> </input></td>
            <td style = "visibility: hidden"><input type = "number" id = "stopnum"> </input></td>
            <td style = "visibility: hidden"><input type = "number" id = "custnum"> </input></td>

0 个答案:

没有答案