javascript - 如果字段有值,则更改边框颜色
问题描述
我想根据是否有值来更改输入的边框颜色。我不明白为什么条件不起作用。不是检查该字段是否为空,else
如果在输入中输入了某些内容,是否应该进入该部分?
class Validation
{
constructor(field, noticeMessage) {
this.field = field;
this.noticeMessage = noticeMessage;
}
valid() {
console.log('coucou');
this.field.style.borderColor = "green";
this.noticeMessage.style.display = "none";
return true;
}
invalid() {
this.field.style.borderColor = "red";
this.field.focus();
this.noticeMessage.style.display = "block";
return false;
}
empty() {
if (this.field.value == "") {
this.invalid();
}
else {
this.valid();
}
}
}
const titleValidation = new Validation(document.getElementById('name'), document.getElementById('name-notice'));
titleValidation.empty();
<div class="notice">
<ul>
<li id="name-notice">The name input must be filled</li>
</ul>
</div>
<form>
<label>name</label>
<input id="name" type="text" name="name">
</form>
解决方案
推荐阅读
- ruby-on-rails - 如何为具有 uuid 的用户进行 Ahoy gem 跟踪访问?
- python-3.x -
而 POST 请求 - asp.net-mvc - ASPNET MVC 3 上的 WindowsAuthentication 不起作用(IsAuthenticated 始终为假)
- image - Flutter:如何在图像上创建文本,可通过手柄 4 个点调整大小
- accessibility - 网络可访问性 - 在按钮悬停时播放音频
- c - 为什么我的数组的总和(使用指针计算)不正确?
- asp.net-core - 如何迭代表单元素,然后将它们传递给 ASP.NET Core 中的控制器?
- python - 在 Python 中读取 JSON 文件
- primefaces - 如何将 themeSwitcher 与 primefaces 集成到菜单栏中?
- angular - Chrome Headless 83.0.4103.116 (Linux x86_64) 错误已断开连接,因为 30000 毫秒内没有消息,Angular 8