我目前正在处理一些代码,如果找到无效的输入,则打开容器,否则什么都不做。
我有一些逻辑,但容器的click事件总是打开和关闭,无论我试图用什么属性值来防止这种情况。我想只在容器尚未打开的情况下才触发click事件。
我正在使用bootstrap来实现折叠功能。
$(document).on("click", ".test", function() {
var inputs = document.querySelectorAll('form div input');
[].forEach.call(inputs, function(input) {
input.addEventListener('invalid', function(e) {
var collaspe = $(this).closest('.panel').find('.panel-heading .collapse-icon').attr('aria-expanded');
console.log(collaspe);
if (!collaspe) {
$(this).closest('.panel').find('img').trigger("click");
} else {
alert("Do Nothing");
}
});
});
});
.container {
width: 75%;
}
.panel-heading {
background-color: #D9DBDE;
padding: 20px;
margin-bottom: 10px;
border-radius: 0;
}
.panel-title {
font-size: 21px;
display: block;
width: 100%;
color: #4F5858;
line-height: 1.3;
font-weight: normal;
}
.collapse-icon {
float: right;
}
.fieldpos {
margin-left: 0px;
width: 60%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<form action="/update" id="formwrite">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title va-middle">Account Settings
<img src='../images/colopen.svg' data-swap='../images/coll.svg' class="panel-icon collapse-icon" role="button" data-toggle="collapse" data-parent="#accordion" href="#collaspeOne" aria-expanded="true" aria-controls="collaspeOne">
</h4>
</div>
<div id="collaspeOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-default">
<div class="row fieldpos">
<fieldset class="form-group textwide">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" placeholder="Enter Email" required>
</fieldset>
</div>
</div>
</div>
</div>
<div class="panel">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title va-middle">Other Settings
<img src='../images/colopen.svg' data-swap='../images/coll.svg' class="panel-icon collapse-icon" role="button" data-toggle="collapse" data-parent="#accordion" href="#collaspeTwo" aria-expanded="true" aria-controls="collaspeTwo">
</h4>
</div>
<div id="collaspeTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-default">
<div class="row fieldpos">
<fieldset class="form-group textwide">
<label for="group">Test</label>
<input type="text" class="form-control " id="group" placeholder="test" required>
</fieldset>
</div>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-success form-control savechanges">Save Changes</button>
<button class="btn btn-success form-control test">Call Function</button>
</form>
</body>
有关失踪的建议吗?
答案 0 :(得分:0)
以下能够完成我需要的东西我只是在另一个参数中添加并查找了类中以确定触发事件是否应该运行。
$(document).on("click", ".savechanges", function() {
$("input[required]").on("invalid", function(event) {
var collaspe = $(this).closest('.collapse').hasClass("in");
console.log(collaspe);
if (!collaspe) {
$(this).closest('.panel').find('img').trigger("click");
} else if (collaspe) {
$("input[required]").off();
} else {
console.log("Do Nothing");
return false;
}
});
});