javascript - JS display div on click of disabled input
问题描述
I'm trying to show the .hideme
message on click of submit
only when it is disabled.
And the .hideme
should be unique for each submit
. I'm not sure if I should use data-attributes to associate them.
$(document).ready(function() {
$("#post-1").hide();
$("#post-1").click(postNotification);
});
function postNotification() {
$("#post-1")
.show()
.animate({
height: "30px",
opacity: 1
}, 250)
.delay(2000)
.animate({
height: "0px",
opacity: 0
}, 250);
}
.hideme {
background: blue;
height: 0px;
opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first">
<div class="hideme" id="post1">Message 1</div>
<input type="text" name="name" autocomplete="off" required/>
<input type="submit" data-submit="1" id="post1" disabled></input>
</div>
<div class="second">
<div class="hideme" id="post2">Message 2</div>
<input type="text" name="name" autocomplete="off" required/>
<input type="submit" data-submit="2" id="post2"></input>
</div>
解决方案
而不是使用(禁用),您可以使用 css 使元素似乎被禁用
.disabled
{
color:#C0C0C0;
}
然后将此类添加到输入而不是禁用
<input type="submit" data-submit="1" class="btn disabled" id="post1" ></input>
并在 jquery 中检测该元素已启用或禁用类并决定应该执行哪个操作
$(document).ready(function() {
$(".btn").click(
function(event){
if($(event.target).hasClass("disabled")){
event.target.parentElement.querySelector(".hideme").style.opacity="1";
event.target.parentElement.querySelector(".hideme").style.height="30";
}else{
alert("action");
}
});
});
推荐阅读
- javascript - 检查数组中的元素是否等于其他数组中的元素
- javascript - 如何结合代码背后和 javascript 函数?
- reactjs - React Admin 中的关联资源列表
- laravel - Laravel - Jenssegers MongoDB 嵌套数组更新不起作用
- ios - SwiftUI:在带有 Spacers 的 VStack 中平均分配多个 VStack
- database - 应该添加冗余列还是应该遵循 3NF?如何设计与父表和子表的关系并仍然强制完整性?
- c# - 身份角色未添加到用户
- jquery - 单击时备用 SVG 填充颜色
- python - 如何从 Python 中的语法中脱糖?
- php - PHP Ajax 查询 - 将多个文件上传为 blob