gpt4 book ai didi

html - 存在文本时在输入框中插入复选图标

转载 作者:行者123 更新时间:2023-11-28 00:55:42 25 4
gpt4 key购买 nike

当文本位于输入框中时,我试图插入一个绿色复选图标。我试图通过添加一个 fa-fa 图标来做到这一点,但它似乎不起作用。此外,如果电子邮件无效,我想在输入框中放置一个红色的 x 图标。如果有人对如何执行此操作有任何见解,将不胜感激!附上 fiddle 和图像。

Green Check and Rex x icons

JsFiddle

<form action="">
<div class="container" id="container">
<label>First Name
<input id="first_name" maxlength="40" name="first_name" size="20" type="text"><i class="fa fa-check-circle" aria-hidden="true"></i>
</label>
<label>Last Name
<input id="last_name" maxlength="80" name="last_name" size="20" type="text">
</label>
<label>Email
<input id="email" maxlength="80" name="email" size="20" type="text"><i class="fa fa-times-circle-o" aria-hidden="true"></i>
<!-- <span class="msg error">Wrong email!</span>
<span class="msg success">Correct email!</span> -->
</label>
<label>Phone
<input id="phone" maxlength="40" name="phone" size="20" type="text">
</label>
<label>City
<input id="city" name="city" maxlength="40" size="20" type="text">
</label>
<label>State/Province
<input id="state" maxlength="20" name="state" size="20" type="text">
</label>
<label id="company">Company
<input id="company" name="company" type="text">
</label>
<label>Comments
<textarea name="" id="" cols="30" rows="10"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</label>
</div>
</form>

body {
color: #fff;
background-color: #f78e2a;
text-align: center;
}

form {
color: #fff;
background-color: #f78e2a;
text-align: center;
font-family: Lato;
}

* {
box-sizing: border-box;
}

h1 {
font-size: 20px;
text-align: center;
}


input[type="text"] {
width: 100%;
padding: 10px;
background-color: #f9a558;
border: 1px solid #fff;
}

input[type="text"]:focus {
background-color: #fff;
}

input[type="text"]:visited {
background-color: #fff;
}
.container {
display: flex;
flex-direction: column;
padding: 5px 0;
margin-left: 10%;
margin-right: 10%;
}

textarea {
width:100%;
background-color: #f9a558;
border: 1px solid #fff;
}

textarea:focus {
background-color: #fff;
}

#company {
flex-basis: 100%;
max-width: 100%;
}

label:nth-last-child(-n+2)
{
flex-basis: 100%;
max-width: 100%;
}


select, label {
height: 50px;
width: 48%;
margin: 2% 1%;
text-align: left;
}


button {
margin-top: 10px;
background-color: #B9B9B9;;
color: #959595;
border-radius: 6px;
width: 120px;
height: 35px;
margin-left: 1%;
display: block;
}

.fa fa-check-circle {
color: green;
}

button:focus{
background-color: #fff;
color: #f78e2a;
}




@media (max-width: 426px) {
label {
width: 98%;
}

}

@media (min-width: 426px) {
.container {
flex-direction: row;
flex-wrap: wrap;
align-self: flex-start;
}


}

最佳答案

使用 javascript 执行此操作的一种方法是检查 input 上的 keyup,如果有值,则显示图标。我在下面使用 jQuery。

对于您的电子邮件,您需要使用某种验证模式 - 如果电子邮件输入有内容,则默认显示红色图标,但如果它与验证模式匹配,则将图标更改为绿色。我从 How to validate email address in JavaScript? 得到了电子邮件验证正则表达式

function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}

$('input[type="text"]').on('keyup', function() {
if ($(this).val().trim() != '') {
if ($(this).is('#email')) {
if (validateEmail($(this).val())) {
$(this).attr('data-valid','valid');
} else {
$(this).attr('data-valid','error');
}
} else {
$(this).attr('data-valid','valid');
}
} else {
$(this).removeAttr('data-valid');
}
});
body {
color: #fff;
background-color: #f78e2a;
text-align: center;
}

form {
color: #fff;
background-color: #f78e2a;
text-align: center;
font-family: Lato;
}

* {
box-sizing: border-box;
}

h1 {
font-size: 20px;
text-align: center;
}

input[type="text"] {
width: 100%;
padding: 10px;
background-color: #f9a558;
border: 1px solid #fff;
}

input[type="text"]:focus {
background-color: #fff;
}

input[type="text"]:visited {
background-color: #fff;
}

.container {
display: flex;
flex-direction: column;
padding: 5px 0;
margin-left: 10%;
margin-right: 10%;
}

textarea {
width: 100%;
background-color: #f9a558;
border: 1px solid #fff;
}

textarea:focus {
background-color: #fff;
}

#company {
flex-basis: 100%;
max-width: 100%;
}

label:nth-last-child(-n+2) {
flex-basis: 100%;
max-width: 100%;
}

select,
label {
height: 50px;
width: 48%;
margin: 2% 1%;
text-align: left;
}

button {
margin-top: 10px;
background-color: #B9B9B9;
;
color: #959595;
border-radius: 6px;
width: 120px;
height: 35px;
margin-left: 1%;
display: block;
}

.fa fa-check-circle {
color: green;
}

button:focus {
background-color: #fff;
color: #f78e2a;
}

@media (max-width: 426px) {
label {
width: 98%;
}
}

@media (min-width: 426px) {
.container {
flex-direction: row;
flex-wrap: wrap;
align-self: flex-start;
}
}

label {
position: relative;
}

.fa {
position: absolute;
bottom: 0;
right: 0;
transform: translate(-50%, -45%);
opacity: 0;
transition: opacity .5s, color .5s;
}

[data-valid] + .fa {
opacity: 1;
}

[data-valid="valid"] + .fa {
color: green;
}

[data-valid="error"] + .fa {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
<div class="container" id="container">
<label>First Name
<input id="first_name" maxlength="40" name="first_name" size="20" type="text"><i class="fa fa-check-circle" aria-hidden="true"></i>
</label>
<label>Last Name
<input id="last_name" maxlength="80" name="last_name" size="20" type="text">
</label>
<label>Email
<input id="email" maxlength="80" name="email" size="20" type="text"><i class="fa fa-times-circle-o" aria-hidden="true"></i>
<!-- <span class="msg error">Wrong email!</span>
<span class="msg success">Correct email!</span> -->
</label>
<label>Phone
<input id="phone" maxlength="40" name="phone" size="20" type="text">
</label>
<label>City
<input id="city" name="city" maxlength="40" size="20" type="text">
</label>
<label>State/Province
<input id="state" maxlength="20" name="state" size="20" type="text">
</label>
<label id="company">Company
<input id="company" name="company" type="text">
</label>
<label>Comments
<textarea name="" id="" cols="30" rows="10"></textarea>
<button type="submit" name="submit">SUBMIT</button>
</label>
</div>
</form>

关于html - 存在文本时在输入框中插入复选图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com