gpt4 book ai didi

javascript - 如果 AngularJS 复选框被过滤掉然后返回,则它们不会保持选中状态

转载 作者:行者123 更新时间:2023-12-03 10:57:51 24 4
gpt4 key购买 nike

我有一个带有复选框的列表,可以使用文本框作为搜索字段来过滤这些复选框

<input type="search" id="valSearch" value="" placeholder="Please type name..." class="search-input" ng-model="query">  
<ul class="list">
<li class="list__item list__item--tappable" ng-repeat="name in classList | filter:query">
<label class="checkbox checkbox--list-item">
<input type="checkbox" class="chkProperty" value="{{id.childName}}">
{{id.childName}}
</label>
</li>
</ul>

我遇到的问题是,如果选择一个复选框然后进行搜索,一旦清除搜索字段并显示整个列表,则选择的复选框在返回时不会保持选中状态(如果它们被隐藏)在搜索过程中。我尝试使用 jquery 在返回时选中这些框,如下所示

$('#valSearch').on('keypress', function () {
setTimeout(function () {
$.each($(".chkProperty"), function (i, e) {
if (nameList[i] == e.value) { //nameList is an array that contains all the selected items
$(this).attr("checked", true);
}
});
}, 1000);
});

最佳答案

当过滤器未返回项目时,

ng-repeat 将删除 DOM 及其所有引用。因此,如果该项目被选中,那么当它被重新插入到 DOM 中时,它就不再被检查,因为它是一个全新的 DOM(而不是旧的 DOM)。

要解决这个问题,您需要让数据(nameList)也保留 state of the item (选中/未选中)。这样每个输入都可以看到它是否在 ng-repeat 内部被检查。为此,只需让输入使用 ng-model

plunker example

关于javascript - 如果 AngularJS 复选框被过滤掉然后返回,则它们不会保持选中状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28200551/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com