0
我對Knockout非常陌生,甚至使用Knockout驗證更新。我有一些簡單的必要輸入,應該防止提交表單。它仍然提交空輸入。我不確定我做錯了什麼。基因敲除驗證不適用於所需項目
我創建了一個小提琴here。要進行測試,請在光標位於上次零售輸入時點擊選項卡。這將添加一個新的空白行。然後點擊提交併查看控制檯。 這裏是我的javascript代碼:
var viewModel;
ko.validation.rules.pattern.message = 'Invalid.';
ko.validation.init({
registerExtenders: true,
messagesOnModified: true,
insertMessages: true,
parseInputAttributes: true,
messageTemplate: null
}, true);
var itemModel = function() {
var self = this;
self.itemNo = ko.observable().extend({required: true});
self.brocCode = ko.observable().extend({required: true});
self.itemDesc = ko.observable().extend({required: true});
self.retail = ko.observable().extend({required: true});
}
var itemsModel = function(items) {
var self = this;
//self.items = ko.observableArray(items);
self.items = ko.mapping.fromJSON(items);
self.checkItemNo = function(data) {
console.log("blurred!");
var itemNo = "abc";
if (itemNo != "") {
var item = "";
/*
$.each(fullItemList, function(i, v) {
if (v.No.search(itemNo) != -1) {
item = v.Description;
return;
}
});
if(item != "") {
console.log("found: " + item);
var match = ko.utils.arrayFirst(self.items, function(item) {
return itemNo === item.itemNo;
});
*/
var match = false;
item = "Mudguard front";
if (!match) {
console.log("not a match!");
console.log(data);
data.itemDesc(item);
}
}
}
self.addLine = function() {
self.items.push(new itemModel())
};
self.removeItem = function(item) {
self.items.remove(item);
};
self.errors = ko.validation.group(self.items);
};
ko.bindingHandlers.enterPress = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var allBindings = allBindingsAccessor();
element.addEventListener('keydown', function (event) {
var keyCode = (event.which ? event.which : event.keyCode);
if (keyCode === 13 || (!event.shiftKey && keyCode === 9)) {
event.preventDefault();
console.log("hit enter/tab!");
bindingContext.$root.addLine();
return false;
}
return true;
});
}
};
function GetItems() {
//var itemsJSON = @Html.Raw(Json.Encode(Model.brochureItems));
var itemsJSON = '[{"brochureId":1,"itemNo":"1000","brocCode":"1000","itemDesc":"Bicycle","retail":13.5},{"brochureId":1,"itemNo":"1100","brocCode":"1100","itemDesc":"Front Wheel","retail":35},{"brochureId":1,"itemNo":"1120","brocCode":"1120","itemDesc":"Spokes","retail":12.5},{"brochureId":1,"itemNo":"1150","brocCode":"1150","itemDesc":"Front Hub","retail":5},{"brochureId":1,"itemNo":"1151","brocCode":"1151","itemDesc":"Axle Front Wheel","retail":14},{"brochureId":1,"itemNo":"120","brocCode":"120","itemDesc":"Loudspeaker, Black, 120W","retail":12.5},{"brochureId":1,"itemNo":"125","brocCode":"125","itemDesc":"Socket Back","retail":10}]';
viewModel = new itemsModel(itemsJSON);
ko.applyBindings(viewModel);
}
$(document).ready(function() {
GetItems();
$('#brochureForm :submit').on('click', function(e) {
e.preventDefault(); //prevent form from submitting
if (viewModel.errors().length === 0)
//if(viewModel.validationModel.isValid())
{
console.log("submitting!");
//$("#brochureForm").submit();
}
else
{
console.log("not valid!");
}
});
});
哇!謝謝!我覺得這是一些設置我錯了或失蹤。他們的文檔缺乏,我希望他們的網站上有更多的例子。 – dmikester1
我可以請你解釋一下這段代碼嗎? 'var mapping = { create:function(options){ return new itemModel(options.data);' – dmikester1
不客氣。在上面的答案中查看** update 2 ** –