我正在处理HTML表单。我在一些输入字段中添加了必需的元素。因为我使用Bootstrap,所以输入字段在焦点时默认为蓝色。我已将所需的输入字段设置为input:required:focus {border: 1px solid red;}
的红色。现在,当填写所需的输入字段时,我想将输入字段边框更改为绿色。我的问题是,这可能与CSS有关吗?如果没有,我怎样才能通过Javascript或其他方式实现这一目标?
input:required:focus {
border: 1px solid red;
outline: none;
}
textarea:required:focus {
border: 1px solid red;
outline: none;
}
.form-horizontal {
padding-left: 15px;
padding-right: 15px;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<form class="form-horizontal">
<div class="form-group">
<label class="col-md-2 control-label" for="Name">
Full name <span class="required">*</span>
</label>
<div class="col-md-10">
<input id="Name" name="Name" placeholder="Full name" class="form-control input-md" required="" type="text">
<span class="format-block"><a data-toggle="tooltip" data-placement="right" title="John Doe">Format</a></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="email">
E-mail <span class="required">*</span>
</label>
<div class="col-md-10">
<input id="email" name="email" placeholder="E-mail" class="form-control input-md" required="" type="email">
<span class="format-block"><a data-toggle="tooltip" data-placement="right" title="johndoe@gmail.com">Format</a></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="phone">
Phone number
</label>
<div class="col-md-10">
<input id="phone" name="phone" placeholder="Phone number" class="form-control input-md" type="text">
<span class="format-block"><a data-toggle="tooltip" data-placement="right" title="Only numbers">Format</a></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="question">
Question <span class="required">*</span>
</label>
<div class="col-md-10">
<textarea rows="20" cols="40" class="form-control" id="question" required="" name="question"></textarea>
</div>
</div>
<div class="col-md-10 col-md-offset-2">
<button type="submit" class="btn btn-default formbuttonalign">Send mail!</button>
</div>
</form>
&#13;
答案 0 :(得分:3)
您希望什么时候变绿?您立即开始输入,或输入字段失去焦点时?让我假设你想要在它失去焦点时发生这种情况。另一个问题是:当它失去焦点时它应该保持绿色吗?我假设它会,因为我的第一个假设是它只会在失去焦点时变成绿色。如果我的任何假设是错误的,请纠正我
<强>使用Javascript:强>
TreeSet
&#13;
$(function(){
$('input:required,textarea:required').on('blur', function(){
if($(this).val()!==''){ //assuming the form doesn't have some fields populated by default.
$(this).addClass('green-border');
} else {
$(this).removeClass('green-border');
}
});
});
&#13;
.green-border {
border: 1px solid green !important;
}
input:required:focus {
border: 1px solid red;
outline: none;
}
textarea:required:focus {
border: 1px solid red;
outline: none;
}
&#13;
答案 1 :(得分:3)
所以我发现HTML5包含valid
和invalid
选择器。
input:required:focus {
border: 1px solid red;
outline: none;
}
textarea:required:focus {
border: 1px solid red;
outline: none;
}
input:focus:valid {
border: 1px solid green;
outline: none;
}
input:focus:invalid {
border: 1px solid red;
outline: none;
}
input:valid {
border: 1px solid green;
}
您可以将它与required
选择器结合使用来检查它是否有效,如果它填写了AND,如果它的正确就像电话号码只包含数字一样。有趣的部分是它唯一的HTML5&amp; CSS3:)
答案 2 :(得分:0)
在必填字段上使用oninput事件,然后验证其是否已填充。
oninput="if(this.value != ''){ this.style.borderColor='green'; } else{ this.style.borderColor='red'; }"
input:required:focus {
border: 1px solid red;
outline: none;
}
textarea:required:focus {
border: 1px solid red;
outline: none;
}
&#13;
<form class="form-horizontal">
<div class="form-group">
<label class="col-md-2 control-label" for="Name">Full name <span class="required">*</span></label>
<div class="col-md-10">
<input id="Name" name="Name" placeholder="Full name" class="form-control input-md" required="" type="text" oninput="if(this.value != ''){ this.style.borderColor='green'; } else{ this.style.borderColor='red'; }">
<span class="format-block"><a data-toggle="tooltip" data-placement="right" title="John Doe">Format</a></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="email">E-mail <span class="required">*</span></label>
<div class="col-md-10">
<input id="email" name="email" placeholder="E-mail" class="form-control input-md" required="" type="email" oninput="if(this.value != ''){ this.style.borderColor='green'; } else{ this.style.borderColor='red'; }">
<span class="format-block"><a data-toggle="tooltip" data-placement="right" title="johndoe@gmail.com">Format</a></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="phone">Phone number</label>
<div class="col-md-10">
<input id="phone" name="phone" placeholder="Phone number" class="form-control input-md" type="text">
<span class="format-block"><a data-toggle="tooltip" data-placement="right" title="Only numbers">Format</a></span>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="question">Question <span class="required">*</span></label>
<div class="col-md-10">
<textarea rows="20" cols="40" class="form-control" id="question" required="" name="question" oninput="if(this.value != ''){ this.style.borderColor='green'; } else{ this.style.borderColor='red'; }"></textarea>
</div>
</div>
<div class="col-md-10 col-md-offset-2">
<button type="submit" class="btn btn-default formbuttonalign">Send mail!</button>
</div>
</form>
&#13;