【发布时间】:2017-11-29 21:56:40
【问题描述】:
使用 Bootstrap 4。
我有四个文本输入,如果 any 的文本输入包含 any 值,我想为按钮添加一个类。
单击按钮时,我想清除输入值,并从按钮中删除类。
我有一半在工作(点击按钮成功清除所有输入)。
我的代码如下;
$(document).ready(function() {
$("#filterRecords input").on("keyup change", function() {
$("#filterRecords input").each(function() {
var element = $(this);
if (element.val().length > 0) {
$('#resetFilter').addClass('btn-outline-primary');
}
});
});
$('#resetFilter').click(function() {
$('input[type=text]').val('').change();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row filterRecords">
<div class="input-group col-xs-6 col-sm-6 col-md-3 mb-3">
<input type="text" id="searchName" class="form-control" placeholder="Search Name">
</div>
<div class="input-group col-sm-6 col-md-3 mb-3">
<input type="text" id="searchLang" class="form-control" placeholder="Search Language">
</div>
<div class="input-group col-sm-6 col-md-3 mb-3">
<input type="text" id="yearMin" class="form-control start" placeholder="Search Start Year">
</div>
<div class="input-group col-sm-6 col-md-3 mb-3">
<input type="text" id="yearMax" class="form-control end" placeholder="Search End Year">
</div>
</div>
<div class="row justify-content-md-center">
<div class="col-md-auto">
<button class="btn btn-sm btn-default" type="button" id="resetFilter">Reset Filters</button>
<hr>
</div>
</div>
任何帮助将不胜感激。
【问题讨论】:
-
小提琴链接?我没有看到一个
-
你的
filterRecords是一个类而不是一个 ID 。证明<div class="row filterRecords">和$("#filterRecords input")改变这个,你的代码就可以工作了 -
@Andreas 停止了复制粘贴的废话,他清楚地说明了他想要什么
-
When the button is clicked, I would like to clear the input values, and remove the class from the button. I have half of it working (clicking button successfully clears all inputs).有什么不清楚的地方? -
I have four text inputs, if any of the text inputs contain any values, i'd like to add a class to a button.和I have half of it working (clicking button successfully clears all inputs).暗示了他想要和缺少的东西@Andreas
标签: javascript jquery html twitter-bootstrap