验证如何工作

时间:2016-11-15 22:40:40

标签: javascript jquery

我支持API,我需要将验证更新为javascript方法:

function AddCategory() {
    var category = $("#category");
    var subCategory = $("#subcategory");

    if (category.val().length > 0 && subCategory.val().length > 0) {
        var grid = $("#lstCategory").data("kendoGrid");
        var listGrid = $("#lstCategory").data().kendoGrid.dataSource.data();

        var dataS = grid.dataSource;

        if (!FindObjectInList(listGrid, "idSubcategory", subCategory.val())) {
            dataS.add({
                idCategory: category.val(),
                category: $("option:selected", category).text(),
                idSubcategory: subCategory.val(),
                subCategory: $("option:selected", subCategory).text()
            });

          dataS.sync();
        }
        else {
            InfoMessage("Category", "Selected subcategory cannot add again");
        }
    } else {
        WarningMessage("Warning", "Select category and subcategory...");
    }
}

我需要删除此验证:
     InfoMessage("Category", "Selected subcategory cannot add again");

但我不明白这种方法是如何运作的,任何人都可以解释一下吗?此致

1 个答案:

答案 0 :(得分:1)

工作原理:

首先,将listGrid,idSubcategory和从subCategory.val()返回的值传递给FindObjectInList。如果返回null(类别不存在) - 则添加传入的新类别信息。否则,如果函数返回true(类别已存在),则通过InfoMessage函数向用户提供通知。

 if (!FindObjectInList(listGrid, "idSubcategory", subCategory.val())) {
        dataS.add({
            idCategory: category.val(),
            category: $("option:selected", category).text(),
            idSubcategory: subCategory.val(),
            subCategory: $("option:selected", subCategory).text()
        });

      dataS.sync();
    }
    else {
        InfoMessage("Category", "Selected subcategory cannot add again");