gpt4 book ai didi

javascript - 禁用 knockout 中无法正常工作的功能

转载 作者:行者123 更新时间:2023-12-02 16:18:28 24 4
gpt4 key购买 nike

如果字段已被删除,我需要禁用它们,但它无法正常工作。

我创建了一个 JSFiddle:https://jsfiddle.net/mdawood1991/fs1exz6q/

x马克应该删除该项目(禁用文本框),但事实并非如此:

这是我的 View 模型:

var viewModel = {};

function QuoteViewModel() {
var self = this;
self.Quantity = ko.observable();
self.Price = ko.observable();
self.Total = ko.computed(function () {
return this.Price() * this.Quantity();
}, this);
self.isDeleted = ko.observable(false);


self.remove = function (item) {
console.log('A');
item.isDeleted = ko.observable(true);
console.log(item.isDeleted());
}
};

viewModel = new QuoteViewModel();
ko.applyBindings(viewModel);

这是相关的 HTML:

<div class="col-md-3">
<div class="form-group">
<label class="control-label">Price Type</label>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Quantity</label>
<input type="text" class="form-control" data-bind="numeric: Quantity, value: Quantity, valueUpdate: 'afterkeydown', disable: !isDeleted()" />
<!--numeric: number, value: number, valueUpdate: 'afterkeydown'-->
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label class="control-label">Price</label>
<div class="input-group">
<div class="input-group-addon">$</div>
<input type="text" class="form-control" data-bind="numeric: Price, value: Price, valueUpdate: 'afterkeydown'" />
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label class="control-label">Total</label>
<div class="input-group">
<div class="input-group-addon">$</div>
<input type="text" class="form-control" data-bind="textInput: Total" />
</div>
</div>
</div>
<div class="col-md-1"> <span class="glyphicon glyphicon-remove" data-bind="click: remove"></span>

</div>
<pre data-bind="text: ko.toJSON($data, null, 2)"></pre>

当我单击“x”时,它会将 isDeleted 属性更新为 TRUE ,但它没有在我的 HTML ( <pre> <p> ) 标签中更新。

它应该如何工作:

 1. When isDeleted == true (Disable fields) 
2. When isDeleted == false (Enable)

最佳答案

Setting an observable goes by invoking it as a function 。而不是:

item.isDeleted = ko.observable(true);

用途:

item.isDeleted(true);

参见this fiddle进行演示。

关于javascript - 禁用 knockout 中无法正常工作的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29364869/

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