Nar*_*a V 1 javascript arrays knockout.js
如何在添加之前检查重复签名.在下面的场景中,如果没有签名找到列表,我想添加签名.
var Signature = function (name, interestDeclared) {
this.Name = ko.observable(name);
this.RelevantInterest = ko.observable(interestDeclared);
}
viewModel = {
signatures: ko.observableArray([]),
addSignature: function () {
var name = $('#signatureName').val();
var intd = $('#interest').is(':checked');
this.signatures.push(new Signature(name, intd));
},
deleteSignature: function (signature) {
this.signatures.remove(signature);
},
insertWitness: function (signature, position) {
this.signatures.splice(position, 0, signature);
}
};
ko.applyBindings(viewModel, document.getElementById("signatories"));
Run Code Online (Sandbox Code Playgroud)
谢谢, - 娜
您还可以在KO框架中使用一些内置的实用程序函数.我过去用这种技术解决了这个问题:
var name = $('#signatureName').val(),
intd = $('#interest').is(':checked'),
match = ko.utils.arrayFirst(this.signatures(), function (item) {
if (item.Name() === name) {
return selectedCounsel;
}
});
if (!match) {
// This means it wasn't already in our array, so we'll add it.
this.signatures.push(new Signature(name, intd));
}
Run Code Online (Sandbox Code Playgroud)
我也同意@madcapnmckay,不使用jQuery找到值从输入,更新模式,使signatureName和interest绑定到你的模型,并使用这些值.