[英]Trying to reset filters. Clicking the button clears values, but doesn't trigger events
I'm making a filter reset button. 我正在制作一个过滤器重置按钮。 When I click the button all values get cleared/reset as expected, but the events aren't being triggered to reset the view to show all the current results.
当我单击按钮时,所有值都会按预期清除/重置,但不会触发事件以重置视图以显示所有当前结果。 I have event listeners on each column's filter field to apply said filter.
我在每列的过滤器字段上都有事件监听器以应用所述过滤器。
$('#clearFilters').on('click', function () {
console.log("start clear");
$('#columnsearch_1').val('').change();
$('#columnsearch_2').val('').trigger("change");
$('#columnsearch_3 option').prop('selected', function () {
return this.defaultSelected;
});
$('#columnsearch_3').trigger("change");
$('#columnsearch_4 input[type=checkbox]:checked').removeAttr('checked').trigger("click");
});
Unsure if I'm using trigger/change wrong or am misunderstanding it's usage, but as far as I know things look correct. 不确定我是使用触发/更改错误还是误解了它的用法,但据我所知,看起来正确。
Just move your return
to the bottom: 只需将
return
移到底部:
$('#clearFilters').on('click', function () {
console.log("start clear");
$('#columnsearch_1').val('').change();
$('#columnsearch_2').val('').trigger("change");
$('#columnsearch_3 option').prop('selected', function () {
$('#columnsearch_3').trigger("change");
$('#columnsearch_4 input[type=checkbox]:checked').removeAttr('checked').trigger("click");
return this.defaultSelected;
});
});
It doesn't let the code after it to execute. 它不会让其后的代码执行。
I ended up taking out the filter logic on the event listeners I had into their own methods and just called those methods after I reset the values in the fields. 我最终将事件监听器的过滤器逻辑提取到了自己的方法中,并在重置字段中的值后才调用了这些方法。
$('#clearFilters').on('click', function () {
$('#columnsearch_1').val('');
$('#columnsearch_2').val('');
$('#columnsearch_3 option').prop('selected', function () {
return this.defaultSelected;
});
$('#columnsearch_4 input[type=checkbox]:checked').removeAttr('checked');
$('#columnsearch_5').val('');
$('#columnsearch_6 option').prop('selected', function () {
return this.defaultSelected;
});
$('#columnsearch_7 option').prop('selected', function () {
return this.defaultSelected;
});
$('#columnsearch_8 option').prop('selected', function () {
return this.defaultSelected;
});
$('#columnsearch_9').val('');
$('##columnsearch_10 input[type=checkbox]:checked').removeAttr('checked');
Column1Filter(table, '');
Column2Filter(table, '');
Column3Filter(table, '');
Column4Filter(table, '');
Column5Filter(table, '');
Column6Filter(table, '');
Column7Filter(table, '');
Column8Filter(table, '');
Column9Filter(table, '');
Column10Filter(table, '');
});
Behaves as intended. 行为符合预期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.