从输入框onblur读取

时间:2012-09-05 13:57:32

标签: javascript html login onblur

我正在尝试构建一个登录页面。我想阅读用户名文本框中的信息,以便在javascript中使用onblur事件。理想情况下,当某人在输入姓名后标记或点击时,页面将以名称读取,并在脚本中使用它来显示有关该人的信息。所有这些都将在输入密码之前发生。有人知道以这种方式从输入框中读取信息的方法吗?这是我到目前为止所拥有的。

//This will display the content
function createDiv()
{
var divTag = document.createElement("div");
divTag.style.position = "absolute";
divTag.style.top = "95px";
divTag.style.left = "426px";
divTag.style.zIndex = "20";
divTag.innerHTML = "MSS";
document.getElementById('card').appendChild(divTag);
}
</script>
</head>

<body onload='init();'>

<div id="card" style="width:800px; z-index:10; height:600px; position:relative; margin-left:auto; margin-right:auto; text-align:center; margin-top:0px; background-image:url('customLoginBG.png'); background-repeat:no-repeat;">
<form method='post' action='/login'>
<ul style="list-style-type:none;">
<li> <input type='hidden' id='token' name='token' /> </li>

<!-- After tabbing away from here, I display content. -->
<li style="position:absolute; top:270px; left:260px;"> <input type='text' onblur="createDiv()" id='username' style="width:188px; height:26px; outline: none; border: none; border-color:transparent; background-image:url('inputBar.png'); " /> </li>

<li style="position:absolute; top:310px; left:260px;"> <input type='password' id='password' style="width:188px; height:26px; outline: none; border: none; border-color:transparent; background-image:url('inputBar.png'); " /> </li>
<li style="position:absolute; top:290px; left:455px;"> <input type='image' src="loginButton.png" value="Login" onclick='doLogin();' /> </li>      

</ul>  
</form>
</div>
</body>

4 个答案:

答案 0 :(得分:1)

使用jquery(1.7或更高版本),你只需

$("#inputboxname").on("blur", function () {
    var value = $(this).val();
    // Do stuff with value
}

答案 1 :(得分:0)

$('input').blur(function() { console.log($(this).val()); }

答案 2 :(得分:0)

您可以将onblur事件处理程序分配给输入:

<input id="username" type="text" />

​document.getElementById("username").onblur = function () {
  var name = this.value;
  // do something with it
};​​​

答案 3 :(得分:0)

document.getElementById('txtbox1').onblur = function(){
     alert(this.value);
}​​​​​​

然而,如果你要做很多javascript的东西,我会学会使用jQuery库,让很多事情变得更加简单。但是理解标准javascript的基础是必须的。