gpt4 book ai didi

javascript - 单击不适用于可观察的数组元素

转载 作者:行者123 更新时间:2023-12-03 00:41:29 25 4
gpt4 key购买 nike

我是 knockout 新手,并尝试构建一个简单的应用程序,在其中可以动态添加数据行,但我不明白为什么删除按钮中的data-bind =“click:remove”是当我从删除按钮中删除data-bind="click:remove"时,我的应用程序将正常工作,

如何使用删除按钮删除动态元素?

function people(name) {
return {
name: ko.observable(name),
remove: function () {
viewModal.peoples.remove(this);
}
};
}
var viewModal = {
self: this,
firstName: ko.observable(),
peoples: ko.observableArray([
{ name: 'Franklin' },
{ name: 'Mario' },

]),
addMore: function () {
this.peoples.push(new people(this.firstName()));
}
};
ko.applyBindings(viewModal);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<input type="text" data-bind="value:firstName" />
</div>
<div class="col-md-2">

</div>
</div>

<input type="button" value="Add People" data-bind="click:addMore" />
</div>
<div class="col-md-12" data-bind="foreach: peoples">
<div class="row">
<div class="col-md-6 form-group">
<label data-bind="text:name"></label>
<input class="form-control" type="text" />
</div>
<div class="col-md-6 form-group">
<input type="button" class="btn btn-primary" value="remove" data-bind="click: remove" />
</div>
</div>
</div>

</div>

最佳答案

编辑:如@GKB在评论中指出,主要错误是最初的 people 对象缺少 remove 方法,结果我已经修复了这个方法,而没有在我的答案中真正指出这一点......

原始答案:

您不能像您尝试过的那样使用this

使用普通对象或使用类实例:

工作示例:

// Example 1: using plain objects
function Person(name, app) {
const self = {
name: ko.observable(name),
remove: () => app.people.remove(self)
};

return self;
}

// Example 2: using `new`
var App = function() {
const self = this;

this.firstName = ko.observable(),
this.people = ko.observableArray(
[ "Franklin", "Mario" ].map(
name => Person(name, self)
)
);
};

App.prototype.addPerson = function() {
this.people.push(Person(this.firstName(), this));
this.firstName("");
};

ko.applyBindings(new App());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<input type="text" data-bind="value: firstName" />
</div>
<div class="col-md-2">

</div>
</div>

<input type="button" value="Add People" data-bind="click: addPerson" />
</div>
<div class="col-md-12" data-bind="foreach: people">
<div class="row">
<div class="col-md-6 form-group">
<label data-bind="text:name"></label>
<input class="form-control" type="text" data-bind="textInput: name" />
</div>
<div class="col-md-6 form-group">
<input type="button" class="btn btn-primary" value="remove" data-bind="click: remove" />
</div>
</div>
</div>

</div>

关于javascript - 单击不适用于可观察的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53445920/

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