本文介绍了检查重复使用ko.observableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何加入之前,我可以检查重复签名。在下面的情况下,我想补充的签名,如果没有签名列表中找到。

How can I check the duplicate signatures before adding. In the below scenario, I wanted add signature if no signature found the list.

    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"));

谢谢,
-Naren

Thanks,-Naren

推荐答案

使用jQuery的grep的功能:

Using jQuery's grep function:

addSignature: function () {
    var name = $('#signatureName').val();
    var intd = $('#interest').is(':checked');
    if($.grep(this.signatures(), function(el){return el.Name() === name;}).length === 0){
        this.signatures.push(new Signature(name, intd));
    }
}

这篇关于检查重复使用ko.observableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 18:35