0

我已经设置了一个validateForm()包含很多 if 的函数。因为按下提交按钮时所有的错误字段都必须被涂成红色,所以我决定在我的 JavaScript 中尝试以下操作:

function validateForm()
{
    var i = 0;

    var a = document.forms["MyForm"]["Naam"].value;
    if(a == null || a == "")
    {
        i++;
        ctrl.style.backgroundColor = #FF0000;
    }

    //alot more if statements like the above

    if(i > 0)
    {
        return false;
    }
}

但是当我点击提交按钮时,这些字段不会被着色。如果我在第一个 if 中放置一个警报语句,它会在提交时字段为空时发出警报。所以验证功能都有效。但是上色不会。有谁知道如何,或者任何人都可以在正确的方向上帮助我吗?

4

1 回答 1

0

试试这个代码。

function checkme_station() 
{
    stationObj = document.getElementById('station_name');
    if (stationObj.value == "")
    {
        stationObj.style.borderColor = '#ff0000';
        alert("Please enter a name for this station.");
        stationObj.focus();
        return false;
    }

    selectionObj = document.getElementById('selection');
    if (selectionObj.value == "Enter song or musician name" || selectionObj.value == "")
    {
        selectionObj.style.borderColor = '#ff0000';
        alert("Please enter the name of the song or name of musician into selection field.");
        selectionObj.focus();
        return false;
    }

    stationObj.style.borderColor = '';
    selectionObj.style.borderColor = '';
    return true;
}
于 2013-10-23T09:46:43.107 回答