2015-09-02 64 views
0

我目前正在將表單重置爲默認初始值後發佈。 我已經嘗試並努力解決問題,但隨着表單字段的增長,它變得繁瑣和重複。因此,我正在尋找一種更通用的解決方案,它可以將表單重置爲初始ko.observable值集(無論是否爲空),而無需重置表單的值,如下所示。希望有人能夠就此舉手。在崗位後重置表單值

function ViewModel() { 
 
    var self = this; 
 
    self.valueA = ko.observable(); // for textbox,textarea.... binding 
 
    self.valueB = ko.observable("Hello"); // for textbox binding 
 
    self.valueC = // json for select option binding 
 
    self.valueD = // json for for checkbox binding 
 
    self.valueE = 3 // selected select option binding 
 
    ..... 
 
    
 
    self.PostForm = $.ajax({ 
 
     type: 'POST', 
 
     data: {}, 
 
     complete: { 
 
     self.ValueA(null); 
 
     self.ValueB(null); 
 
     $('#checkbox1').val(3); 
 
     ... 
 
     // turn validation message off 
 
     } 
 
    }) 
 
}

回答

0

則可以考慮創建一個模型對象來支持你的形式。重置將是一個剛剛取代新實例的問題:

var Model = function(){ 
    var self = this; 
    self.valueA = ko.observable(); 
    self.valueB = ko.observable(); 
    self.valueC = []; 
    self.valueD = []; 
    self.valueE = ko.observable(3); 
}; 

function ViewModel() { 
    var self = this; 
    self.model = ko.observable(new Model()); 

    self.PostForm = $.ajax({ 
     type: 'POST', 
     data: {}, 
     complete: { 
     self.model(new Model()); 
     ... 
     // turn validation message off 
     } 
    }); 
}