2

我在发布 json 数据时遇到问题,因为我不知道如何在 ajax 回发期间删除其中一个 json 节点(在本例中为 ServiceOptions)。

首先这是我的 HTML:

<h3>Company Profile:</h3>
<ol style="list-style:none;">
    <li>
        Company Name<br />
        <input id="CompanyName" type="text" data-bind="value:CompanyName" />
    </li>
    <li>
        Service Type<br />
        <select id="ServiceType" data-bind="options:ServiceOptions,optionsText:'text',optionsValue:'value',value:ServiceType,optionsCaption:'Choose....'">
        </select>
    </li>
    <li>
        Street<br />
        <input id="Street1" type="text"  data-bind="value:Street1" /><br />
        <input id="Street2" type="text"  data-bind="value:Street2" /><br />
        <input id="Street3" type="text"  data-bind="value:Street3" />
    </li>
    <li>
        Suburb<br />
        <input id="Suburb" type="text" data-bind="value:Suburb" />
    </li>
    <li>
        Post Code<br />
        <input id="PostCode" type="text" data-bind="value:PostCode" />
    </li>
    <li>
        State<br />
        <input id="State" type="text" data-bind="value:State" />
    <li>
        Telephone<br />
        <input id="Telephone" type="text" data-bind="value:Telephone" />
    </li>
    <li>
        Fax<br />
        <input id="Fax" type="text" data-bind="value:Fax" />
    </li>
    <li>
        Status <input id="IsActive" type="checkbox" data-bind="checked: IsActive" />
    </li>
    <li>
        <button data-bind="click:Update_Click">Update</button>
        <button data-bind="click:Delete_Click">Delete</button>
    </li>
</ol>

我的javascript是:

    var model = function () {
        var self = this;
        self.CompanyName = ko.observable('');
        self.ServiceType = ko.observable();
        self.ServiceOptions = ko.observableArray(   
            [
                { value: 0, text: 'Dry Cleaning' },
                { value: 1, text: 'Dog Walking' }
            ]
        );
        self.Street1 = ko.observable('');
        self.Street2 = ko.observable('');
        self.Street3 = ko.observable('');
        self.Suburb = ko.observable('');
        self.PostCode = ko.observable('');
        self.State = ko.observable('');
        self.Telephone = ko.observable('');
        self.Fax = ko.observable('');
        self.IsActive = ko.observable(false);
        this.Update_Click = function () {
            alert(ko.toJSON(self));
        };
        this.Delete_Click = function () {
            alert('delete');
        };
    };

    $(document).ready(function () {
        ko.applyBindings(new model);
    });

当我单击更新按钮时,我得到

"CompanyName":"","ServiceOptions":[{"value":0,"text":"干洗"},{"value":1,"text":"遛狗"}],"Street1" :"","Street2":"","Street3":"","Suburb":"","PostCode":"","State":"","Telephone":"","Fax": "","IsActive":false}

正如您在上面的 JSON 结果中看到的那样,我仍然得到 ServiceOptions。

知道如何解决吗?

4

1 回答 1

3

ko.toJSON在内部使用JSON.stringify您可以指定替换函数的位置。在这个替换函数中,您需要返回undefined您不想包含在最终 JSON 中的键(属性名称):

this.Update_Click = function () {
    alert(ko.toJSON(self, function(key, value) { 
         if (key == "ServiceOptions") 
             return undefined;
         return value;
    }));
};
于 2013-08-13T06:26:11.210 回答