如何在密码输入字段旁边显示div?

问题描述 投票:1回答:1

我正在尝试在输入字段旁边显示密码验证框。此逻辑有效,但具有所有密码要求的Box未显示在正确的位置。它不是位于场地旁边,而是在场地上显示。这是一个例子:

$('input[type=password]').keyup(function() {
	var pswd = $(this).val();
	
    //validate the length
	if ( pswd.length < 8 ) {
	    $('#length').removeClass('valid').addClass('invalid');
	} else {
	    $('#length').removeClass('invalid').addClass('valid');
	}

    //validate letter
	if ( pswd.match(/[A-z]/) ) {
	    $('#letter').removeClass('invalid').addClass('valid');
	} else {
	    $('#letter').removeClass('valid').addClass('invalid');
	}

	//validate capital letter
	if ( pswd.match(/[A-Z]/) ) {
	    $('#capital').removeClass('invalid').addClass('valid');
	} else {
	    $('#capital').removeClass('valid').addClass('invalid');
	}

	//validate number
	if ( pswd.match(/\d/) ) {
	    $('#number').removeClass('invalid').addClass('valid');
	} else {
	    $('#number').removeClass('valid').addClass('invalid');
	}
}).focus(function() {
    $('#pswd_info').show();
}).blur(function() {
    $('#pswd_info').hide();
});
.form-group.required .control-label:after {
  	content: "*";
  	color: red;
}
#pswd_info {
    position:absolute;
    bottom:-75px;
    bottom: -115px\9; /* IE Specific */
    right:55px;
    width:250px;
    padding:15px;
    background:#fefefe;
    font-size:.875em;
    border-radius:5px;
    box-shadow:0 1px 3px #ccc;
    border:1px solid #ddd;
}
#pswd_info h4 {
    margin:0 0 10px 0;
    padding:0;
    font-weight:normal;
}
#pswd_info::before {
    content: "\25B2";
    position:absolute;
    top:-12px;
    left:45%;
    font-size:14px;
    line-height:14px;
    color:#ddd;
    text-shadow:none;
    display:block;
}
.invalid {
    background:url(../images/invalid.png) no-repeat 0 50%;
    padding-left:22px;
    line-height:24px;
    color:#ec3f41;
}
.valid {
    background:url(../images/valid.png) no-repeat 0 50%;
    padding-left:22px;
    line-height:24px;
    color:#3a7d34;
}
#pswd_info {
    display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">

<div class="container">
  <form>
    <div class="form-group required">
      <label class="control-label" for="fname">First Name</label>
      <input type="text" class="form-control" name="temp_fname" id="temp_fname" placeholder="Enter First Name" maxlength="50" required>
    </div>
    <div class="form-group required">
      <label class="control-label" for="lname">Last Name</label>
      <input type="text" class="form-control" name="temp_lname" id="temp_lname" placeholder="Enter Last Name" maxlength="50" required>
    </div>
    <div class="form-group required">
      <label class="control-label" for="username">UserName</label>
      <input type="text" class="form-control" name="temp_username" id="temp_username" placeholder="Enter UserName" maxlength="50" required>
    </div>
    <div class="form-group required">
      <label class="control-label" for="password">Password</label>
      <input type="password" class="form-control" name="temp_password" id="temp_password" placeholder="Enter Password" maxlength="64" required>
      <small id="passwordHelp" class="form-text text-muted">Special characters are allowd:  <span class="text-danger">!"#$%&'()*+,-.:;<=>?@[\]^_`{|}~</span></small>
    </div>
    <div class="form-group required">
      <label class="control-label" for="email">Email address</label>
      <input type="email" class="form-control" name="temp_email" id="temp_email" placeholder="Enter email" maxlength="80" required>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>
</div>
<div id="pswd_info">
  <h4>Password must meet the following requirements:</h4>
  <ul>
    <li id="letter" class="invalid">At least <strong>one letter</strong></li>
    <li id="capital" class="invalid">At least <strong>one capital letter</strong></li>
    <li id="number" class="invalid">At least <strong>one number</strong></li>
    <li id="length" class="invalid">Be at least <strong>8 characters</strong></li>
  </ul>
</div>

有没有办法在密码输入字段旁边显示密码框?如果框位于输入字段的右侧,框中的箭头也可以从框的左侧指向?图像有问题应该替换子弹。我不知道为什么会显示子弹和图像。只有图像应该在那里。如果有人知道为什么我的代码破产请告诉我。

javascript jquery html css twitter-bootstrap
1个回答
1
投票

试试这个:

$('input[type=password]').keyup(function() {
	var pswd = $(this).val();
	
    //validate the length
	if ( pswd.length < 8 ) {
	    $('#length').removeClass('valid').addClass('invalid');
	} else {
	    $('#length').removeClass('invalid').addClass('valid');
	}

    //validate letter
	if ( pswd.match(/[A-z]/) ) {
	    $('#letter').removeClass('invalid').addClass('valid');
	} else {
	    $('#letter').removeClass('valid').addClass('invalid');
	}

	//validate capital letter
	if ( pswd.match(/[A-Z]/) ) {
	    $('#capital').removeClass('invalid').addClass('valid');
	} else {
	    $('#capital').removeClass('valid').addClass('invalid');
	}

	//validate number
	if ( pswd.match(/\d/) ) {
	    $('#number').removeClass('invalid').addClass('valid');
	} else {
	    $('#number').removeClass('valid').addClass('invalid');
	}
}).focus(function() {
    $('#pswd_info').show();
}).blur(function() {
    $('#pswd_info').hide();
});
.form-group.required .control-label:after {
  	content: "*";
  	color: red;
}

.password-container {
  position: relative;
}
#pswd_info {
    position:absolute;
    top: 85px;
    left: 25px;
    width:250px;
    padding:15px;
    background:#fefefe;
    font-size:.875em;
    border-radius:5px;
    box-shadow:0 1px 3px #ccc;
    border:1px solid #ddd;
}
#pswd_info h4 {
    margin:0 0 10px 0;
    padding:0;
    font-weight:normal;
}
#pswd_info::before {
    content: "\25B2";
    position:absolute;
    top:-12px;
    left:45%;
    font-size:14px;
    line-height:14px;
    color:#ddd;
    text-shadow:none;
    display:block;
}
.invalid {
    background:url(../images/invalid.png) no-repeat 0 50%;
    padding-left:22px;
    line-height:24px;
    color:#ec3f41;
}
.valid {
    background:url(../images/valid.png) no-repeat 0 50%;
    padding-left:22px;
    line-height:24px;
    color:#3a7d34;
}
#pswd_info {
    display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">

<div class="container">
  <form>
    <div class="form-group required">
      <label class="control-label" for="fname">First Name</label>
      <input type="text" class="form-control" name="temp_fname" id="temp_fname" placeholder="Enter First Name" maxlength="50" required>
    </div>
    <div class="form-group required">
      <label class="control-label" for="lname">Last Name</label>
      <input type="text" class="form-control" name="temp_lname" id="temp_lname" placeholder="Enter Last Name" maxlength="50" required>
    </div>
    <div class="form-group required">
      <label class="control-label" for="username">UserName</label>
      <input type="text" class="form-control" name="temp_username" id="temp_username" placeholder="Enter UserName" maxlength="50" required>
    </div>
    <div class="form-group required password-container">
      <label class="control-label" for="password">Password</label>
      <input type="password" class="form-control" name="temp_password" id="temp_password" placeholder="Enter Password" maxlength="64" required>
      <small id="passwordHelp" class="form-text text-muted">Special characters are allowd:  <span class="text-danger">!"#$%&'()*+,-.:;<=>?@[\]^_`{|}~</span></small>
      <div id="pswd_info">
        <h4>Password must meet the following requirements:</h4>
        <ul>
          <li id="letter" class="invalid">At least <strong>one letter</strong></li>
          <li id="capital" class="invalid">At least <strong>one capital letter</strong></li>
          <li id="number" class="invalid">At least <strong>one number</strong></li>
          <li id="length" class="invalid">Be at least <strong>8 characters</strong></li>
        </ul>
      </div>
    </div>
    <div class="form-group required">
      <label class="control-label" for="email">Email address</label>
      <input type="email" class="form-control" name="temp_email" id="temp_email" placeholder="Enter email" maxlength="80" required>
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>
</div>
  • 我将弹出窗口的位置移动到保存密码的块内。
  • 我将包含块的position设置为relative。这允许孩子相对于该块定位而不移动块
  • 我根据新的父块将CSS更改为位置
© www.soinside.com 2019 - 2024. All rights reserved.