innerHTML没有隐藏在javascript验证中

时间:2012-11-30 14:34:33

标签: javascript

function editvalidation() {

    var isDataValid = true;

    var currentCourseO = document.getElementById("currentCourseNo");
    var newCourseNoO = document.getElementById("newCourseNo");

    var currentCourseMsgO = document.getElementById("currentAlert");
    var newCourseMsgO = document.getElementById("newAlert");


    if (currentCourseO.value == "") {
        currentCourseMsgO.innerHTML = "Please Select a Course to edit from the Course Drop Down Menu";
        newCourseMsgO.innerHTML = "";
        isDataValid = false; 
    } else {
        currentCourseMsgO.innerHTML = ""; 
    }


    if (newCourseNoO.value == "") {
        newCourseMsgO.innerHTML = "Please fill in the Course ID in your Edit";
        isDataValid = false;
    } else {
        newCourseMsgO.innerHTML = ""; 
    }


    return isDataValid;

}

嗨,在上面的代码中我要做的是,如果满足currentCourseO.value == "",则显示其字符串消息,但不显示newCourseMsgO的字符串消息。

如果未满足currentCourseO.value == "",则显示newCourseMsgO的字符串,如果符合此验证,则显示newCourseMsgO.innerHTML = "Please fill in the Course ID in your Edit";

目前,当newCourseMsgO遇到currentCourseO.value == ""时,它不会隐藏{{1}}的字符串。我可以请用javascript回答。

1 个答案:

答案 0 :(得分:1)

听起来你应该连接两个if-else语句,现在它们并不依赖于彼此。试试这个:

if (currentCourseO.value == "") {
    currentCourseMsgO.innerHTML = "Please Select a Course to edit from the Course Drop Down Menu";
    newCourseMsgO.innerHTML = "";
    isDataValid = false; 
} else {
    if (newCourseNoO.value == "") {
        newCourseMsgO.innerHTML = "Please fill in the Course ID in your Edit";
        isDataValid = false;
    } else{
        newCourseMsgO.innerHTML = ""; 
    }
}