javascript - 如何使用js隐藏输入点击的输入值?
问题描述
我试图在单击时隐藏每个单独输入的输入值
我的 javascript 工作正常,但只针对列出的个人 ID。我正在寻找一种更精细的方法,它只选择单击的输入,而不必针对每个不同的 ID 重复相同的代码块 5 次
$(document).ready(function() {
var text = document.getElementById('first_name');
var button = document.getElementById('first_name');
button.onclick = function() {
text.value = '';
text.classList.add("lowercase");
}
});
input[type=text] {
display: block;
border: none;
border-bottom: 1px solid;
width: 100%;
padding: 2rem 0 1rem;
}
input, input[type=submit] {
font-family: 'usm', sans-serif;
font-size: 0.7rem;
letter-spacing: 1pt;
text-transform: uppercase;
color: #7A7A7A;
}
input.lowercase {
text-transform: none !important;
letter-spacing: 0 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<form method="post" action="">
<input type="text" name="first_name" id="first_name" value="First Name*">
<br>
<input type="text" name="last_name" id="last_name" value="Last Name*">
<br>
<input type="text" name="email" id="email" value="Email Address*">
<br>
<input type="text" name="phone" id="phone" value="Phone Number">
<br>
<input type="text" name="message" id="message" value="Message*">
<div class="submit" >
<input type="submit" name="submit" value="Send" id="submit">
</div><!-- End of Submit -->
</form>
</body>
解决方案
也许您应该考虑改用占位符。
$(document).ready(function() {
$('form input[type="text"]').on('click', function() {
this.value = '';
});
});
input[type=text] {
display: block;
border: none;
border-bottom: 1px solid;
width: 100%;
padding: 2rem 0 1rem;
}
input, input[type=submit] {
font-family: 'usm', sans-serif;
font-size: 0.7rem;
letter-spacing: 1pt;
text-transform: uppercase;
color: #7A7A7A;
}
input.lowercase {
text-transform: none !important;
letter-spacing: 0 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<input type="text" name="palceholder_example" id="placeholdr_example" placeholder="Place holder example">
<form method="post" action="">
<input type="text" name="first_name" id="first_name" value="First Name*">
<br>
<input type="text" name="last_name" id="last_name" value="Last Name*">
<br>
<input type="text" name="email" id="email" value="Email Address*">
<br>
<input type="text" name="phone" id="phone" value="Phone Number">
<br>
<input type="text" name="message" id="message" value="Message*">
<div class="submit" >
<input type="submit" name="submit" value="Send" id="submit">
</div><!-- End of Submit -->
</form>
</body>
推荐阅读
- angular - 找不到不同的支撑物体 angular 7
- ruby-on-rails - Rails 5 可选的 has_many 关联
- amazon-web-services - 遇到不受支持的属性 SourceSecurityGroupId
- reactjs - 反应 Chrome 扩展不呈现
- javafx - 在 Raspberry 上通过 JavaFX 应用程序进行终端打印
- cpu-architecture - 此操作对链操作是否正确?
- javascript - 使用 CSS / Jquery 为隐藏元素创建平滑过渡
- mongodb - Compass 导出一个文档并尝试导入单个文档
- android - EditText - 没有建议的大写单词?
- postgresql - 使用多个值 Postgres 更新列