0

这是我正在尝试进行的留言簿验证。标签中的文本<p>没有改变,我不知道为什么。你能帮我验证这个表格吗:

function Frmvalidate() {
    var nmchk = document.forms["guestform1"]["name"].value;
    var cmntchk = document.forms["guestform1"]["comment"].value;
    if (nmchk == null || nmchk == "") {
        var namep = document.getElementById("namep");
        x.innerHTML = "name must be filled out";
        return false;
    } else {
        return true;
    }
    if (cmntchk == null || cmntchk == "") {
        var cmntp = document.getElementById("cmntp");
        x.innerHTML = "comment must be filled out";
        return false;
    } else {
        return true;
    }
}
4

1 回答 1

2

代替

    var namep = document.getElementById("namep");
    x.innerHTML="name must be filled out";

你可能想要

    var namep = document.getElementById("namep");
    namep.innerHTML="name must be filled out";

但是当你执行第一个时if else,你退出了函数,所以你可能想要这样做:

function Frmvalidate() {
    var nmchk=document.forms["guestform1"]["name"].value;
    var cmntchk=document.forms["guestform1"]["comment"].value;
    if (nmchk==""||nmchk==null) {
            var namep = document.getElementById("namep");
            namep.innerHTML="name must be filled out";
            return false;
    }
    if (cmntchk==""||cmntchk==null){
            var cmntp = document.getElementById("cmntp");
            cmntp.innerHTML="comment must be filled out";
            return false;
    }
    return true;
}
于 2013-07-17T15:09:52.783 回答