English 中文(简体)
Javascript - Value not subsequently to function
原标题:Javascript - Value not passing to function

因此,我有这样的形式:

<form name="login" id="login" action="" method="POST" onSubmit="return test()">
        <input type="text" size="10" name="username" /><div id="wrongUser"></div>
            <br />
        <input type="password" size="10" name="password" /><div id="wrongPass"></div>
            <br />
    <input type="submit" value="submit" name="submit" /><br /><br />
</form>

这两项职能:

function test()
{
    var user = document.login.username.value;
    var pass = document.login.password.value;
    if((user == "" || user == null) && (pass == "" || pass == null))
    {
        document.getElementById( wrongUser ).innerText = "Please Enter Username";
        document.getElementById( wrongPass ).innerText = "Please Enter Password";
        return false;
    }

    if(checkEmpty(user,  wrongUser , "Please Enter Username"))
        return false

    if(checkEmpty(pass,  wrongPass , "Please Enter Password"))
        return false;

    return true;
}

function checkEmpty(name, id, output)
{
    if(name == "" || name == null)
    {
        document.getElementById(id).innerText = "";
        document.getElementById(id).innerText = output;
        return true;
    }
    else
        return false;
}

Now the functions kinda work but not how I would think. If the user only doesn t enter anything (first time) then they get the 2 errors. If the user enter just a username (second time) then the username error should disappear, but it doesn t. If I take it out of the function and replace the variables with their normal values then it works just fine. Why would it change when put in this function?

最佳回答

Put the document.getElementById().innerText = in the other, not in the if. 因为你空洞时只能重新打入内机,但如果它不是空洞的话,你会重新打开该机:

function checkEmpty( name, id, output ) {
  var elem = document.getElementById(id); // it s faster to put the element in a var

  if( name === undefined || name ==    name == null )
    elem.innerText = output;
    return true;
  else
    elem.innerText =   ;
    return false;
}
问题回答

暂无回答




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签