【问题标题】:How to have a JavaScript file go off when a text box is unfocused?当文本框没有焦点时,如何让 JavaScript 文件消失?
【发布时间】:2014-03-04 01:51:08
【问题描述】:

我正在寻找一种方法来让我的 JavaScript 文件在用户不关注我网站上的文本框时关闭。

背景信息:

这是用于检查用户“密码”和“确认密码”字段是否匹配的注册表单。当用户尝试提交注册数据但密码不匹配时,我编写的脚本会将文本框的两个边框都变成红色。

我想要发生的事情:

当用户尝试提交表单后密码不匹配时,我不想让文本框的边框变为红色,我希望在用户不关注“确认密码”文本时触发此脚本框,密码不匹配。

我的代码:

HTML:

<div id="FormInput3">

    <h3>Create Password</h3>

    <input id="pass1" class="signup-textboxes" placeholder="Enter your desired password..." type="password" onfocus="emptyElement('status')" maxlength="100">

</div>

<div id="FormInput4">

    <h3>Confirm Password</h3>

    <input id="pass2" class="signup-textboxes" placeholder="Confirm your password..." type="password" onfocus="emptyElement('status')" maxlength="100">

</div>

<button id="signupbtn" onclick="signup()">Create Account</button>

CSS:

/* ****Password 1**** */

#signupform > #FormInput3 {

    width: 450px;

    height: 40px;

    line-height: 40px;

    margin-left: 20px;

    display: block;

    margin-left: auto;

    margin-right: auto;

}

#signupform > #FormInput3 h3 {

    vertical-align: middle;

    font-family: "Lato Light", Arial;

    color: gray;

    font-size: 20px;

    font-family: "Lato Regular", Arial;

    font-weight: 100;

    margin-left: 24px;

}

#signupform > #FormInput3 > #pass1{

    float: right;

    color: gray;

    margin-top: -60px;

    width: 250px;

    background-color: #000;

    border: none;

    height: 30px;

    margin-right: 5px;

    border-bottom: 1px dotted gray;

    font-size: 15px;

    outline: none;

    -webkit-transition: border-color 0.3s ease;

    -moz-transition: border-color 0.3s ease;

    -ms-transition: border-color 0.3s ease;

    transition: border-color 0.3s ease;

}

#signupform > #FormInput3 > #pass1:hover {

    border-color: #fff;

    color: white;

}

#signupform > #FormInput3 > #pass1:focus{

    border-color: #fff;

    color: white;

}

/* ****Password 2**** */

#signupform > #FormInput4 {

    width: 450px;

    height: 40px;

    line-height: 40px;

    margin-left: 20px;

    display: block;

    margin-left: auto;

    margin-right: auto;

}

#signupform > #FormInput4 h3 {

    vertical-align: middle;

    font-family: "Lato Light", Arial;

    color: gray;

    font-size: 20px;

    font-family: "Lato Regular", Arial;

    font-weight: 100;

    margin-left: 10px;

}

#signupform > #FormInput4 > #pass2 {

    float: right;

    color: gray;

    margin-top: -60px;

    width: 250px;

    background-color: #000;

    border: none;

    height: 30px;

    margin-right: 5px;

    border-bottom: 1px dotted gray;

    font-size: 15px;

    outline: none;

    -webkit-transition: border-color 0.3s ease;

    -moz-transition: border-color 0.3s ease;

    -ms-transition: border-color 0.3s ease;

    transition: border-color 0.3s ease;

}

#signupform > #FormInput4 > #pass2:hover {

    border-color: #fff;

    color: white;

}

#signupform > #FormInput4 > #pass2:focus{

    border-color: #fff;

    color: white;

}

JavaScript:

function signup(){
var p1 = _("pass1").value;
var p2 = _("pass2").value;
if(p1 != p2){

     status.innerHTML = "Your password fields do not match.";

     document.getElementById("pass1").style.borderColor="red";

     document.getElementById("pass1").style.color="red";

     document.getElementById("pass2").style.borderColor="red";

     document.getElementById("pass2").style.color="red";
 }
}

就像我之前提到的,您在此处看到的 JavaScript 文件执行我想要的操作,但仅在用户单击“注册”按钮之后。请让我知道是否可以按照我的要求进行操作。提前致谢! (参考请到http://www.codesrce.com/signup.php

【问题讨论】:

  • 使用blur 事件。

标签: javascript php html css ajax


【解决方案1】:

焦点的反面是模糊..

https://api.jquery.com/blur/

【讨论】:

  • 哎哟。然后..我等待答案。
【解决方案2】:

我会推荐使用jQuery,但是在纯js和html中你可以使用onblur事件

<input id="pass2" class="signup-textboxes" placeholder="Confirm your password..." type="password" onfocus="emptyElement('status')" onblur="signup()" maxlength="100">

【讨论】:

  • 好的,我试试看。如果它不起作用,我可能会求助于使用 jQuery。
  • 为什么要推荐 jQuery 来做一些 javascript 已经可以做到的简单事情?
  • 因为他可能正试图帮助我使我的网站现代化。但不用担心,这就像一个魅力。我很感激帮助。我会尽可能接受这个答案。
  • @Shryme 因为该应用程序可能比普通的注册页面更多,如果您从一开始就添加它而不是尝试添加,使用 jQuery 会使事情变得容易得多在后期转换所有内容
  • 请查看我的网站,看看它是否有效。尝试输入不匹配的密码,然后让它们匹配,看看红色变回灰色。非常感谢。 codesrce.com/signup.php
猜你喜欢
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
相关资源
最近更新 更多