0
function checkvalue() { 
    var areaDesc = document.getElementById('areaDesc').value;
    var cboLeaveType = document.getElementById('cboLeaveType').value;
    var fromDate = document.getElementById('fromDate').value;
    var toDate = document.getElementById('toDate').value;

    if (areaDesc == "" || fromDate == "" || toDate == "" || cboLeaveType = "")
    {            
        alert("empty hoys");            
    }
    else
    {
        document.getElementById('hdnAction').value = "go";
        document.frmLeave.submit();
    }               
}

thats the code, it is working but, I want to alert those area who is still empty,

for example.

ex1: areaDesc, fromDate, toDate is not empty, it must alert "txtSignOff still empty";

ex2: areaDesc, fromDate is not empty it must alert "toDate,txtSignOff still empty"; or

toDate is empty

toDate is empty

4

3 回答 3

1

You can use for in like this:

function checkvalue() { 
    var fields = {
        'areaDesc' : document.getElementById('areaDesc').value;
        'cboLeaveType' : document.getElementById('cboLeaveType').value;
        'fromDate' : document.getElementById('fromDate').value;
        'toDate' : document.getElementById('toDate').value;
    };

    for(var fieldName in fields){
        if(fields[fieldName] == ""){
            alert("field" + fieldName + "is empty");
            return false;
        }
    }

    document.getElementById('hdnAction').value = "go";
    document.frmLeave.submit();
}
于 2013-11-15T06:09:11.247 回答
0

You would have to have an individual if statement for each input type you're requiring, otherwise it's a very generic error message. Something like this should do it:

Also, where is txtSignOff defined? Did you mean cboLeaveType?

If you don't want multiple alerts, just combine all the alerts from the code below and convert them into a string, and then if (error), alert the error string.

function checkvalue() { 
    var areaDesc = document.getElementById('areaDesc').value;
    var cboLeaveType = document.getElementById('cboLeaveType').value;
    var fromDate = document.getElementById('fromDate').value;
    var toDate = document.getElementById('toDate').value;

    if (!areaDesc) alert("areaDesc still empty");
    if (!fromDate) alert("fromDate still empty");
    if (!toDate) alert("toDate still empty");
    if (!txtSignOff) alert("txtSignOff still empty");

    if (areaDesc && fromDate && toDate && txtSignOff)
    {
        document.getElementById('hdnAction').value = "go";
        document.frmLeave.submit();
    }
}
于 2013-11-15T06:01:42.693 回答
0

You may have to split your if block into individual ones, then use a String and concatenate it with the empty field's name in each if. That is,

var emptyStr = ''
if (areaDesc == '') { emptyStr += 'areaDesc,' ; }
if (fromDate == '') { emptyStr += 'fromDate,' ; }...
...
emptyStr += ' still empty'
if (emptyStr != '') { alert(emptyStr); }
于 2013-11-15T06:03:59.293 回答