Javascript函数无法正常工作。编辑

时间:2016-08-11 13:33:46

标签: javascript html

我已经为用户创建了一个注册表,以填写他们的数据并提交注册。如果某些字段为空,用户名不可用,或者该用户的电子邮件已经注册,则会在提交按钮下方显示警告,暂停其进程,直到纠正错误为止。

emailusername输入框下方,分别有一个名为emailstatusunamestatus的范围ID。如果电子邮件或用户名已注册或不可用,则会在该范围内放置一条消息,通知用户。但是,无论是否显示该消息,即使用户的信息没有任何问题,跨度标识status也始终显示消息“请更正您的错误”。可以在javascript函数signup()中找到。请帮忙。

HTML:

<p>Enter personal details</p>
   //cleaned up code
    <input id="email" onfocus="emptyElement('status')" onblur="checkemail()" onkeyup="restrict('email')" maxlength="88" type="text" class="form-control" placeholder="Email"><span id="emailstatus"></span>

<input id="username" type="text" onfocus="emptyElement('status')" onblur="checkusername()" onkeyup="restrict('username')" maxlength="16" class="form-control" placeholder="User Name">
<span id="unamestatus"></span>

<button id="signupbtn" onclick="signup();" class="btn btn-lg btn-login btn-block">Create Account</button>
<span style="font-weight:bold;" id="status"></span>

Javascript:signup()

function signup(){
    var u = _("username").value;
    var e = _("email").value;
    var status = _("status");   

if(unamestatus.innerHTML.value != "" || emailstatus.innerHTML.value != ""){
     status.innerHTML = "Please correct your errors.";
    } else {
        _("signupbtn").style.display = "none";
        status.innerHTML = 'please wait ...';
        var ajax = ajaxObj("POST", "registration.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                if(ajax.responseText != "signup_success"){
                    status.innerHTML = ajax.responseText;
                    _("signupbtn").style.display = "block";
                    location.href = "registration_success.php";
                } 
            }
        }
        ajax.send("fn="+fn+"&ln="+ln+"&u="+u+"&e="+e+"&p="+p1+"&c="+c+"&g="+g);
    }
}

Javascript:checkusername()

function checkusername(){
    var u = _("username").value;
    if(u != ""){
        //_("unamestatus").innerHTML = 'checking ...';
        var ajax = ajaxObj("POST", "registration.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                _("unamestatus").innerHTML = ajax.responseText;
            }
        }
        ajax.send("usernamecheck="+u);
    }
}

Javascript:checkemail()

function checkemail(){
    var e = _("email").value;
    if(e != ""){
        //_("emailstatus").innerHTML = 'checking ...';
        var ajax = ajaxObj("POST", "registration.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                _("emailstatus").innerHTML = ajax.responseText;
            }
        }
        ajax.send("emailcheck="+e);
    }
}

1 个答案:

答案 0 :(得分:0)

你的问题在这里:

} else if(unamestatus.innerHTML.value != "" || emailstatus.innerHTML.value != ""){
    status.innerHTML = "Please correct your errors.";
}

innerHTML没有value属性。使用,

} else if(unamestatus.innerHTML != "" || emailstatus.innerHTML != ""){
    status.innerHTML = "Please correct your errors.";
}
相反,不过有更好的方法可以做到这一点。