KnockoutJS 将模型更改为控制器

KnockoutJS Changes Model to Controller

我有一个使用 foreach 绑定到 videmodel 的下拉框

<td>
                        <input class='required' type="text" data-bind="value: muhasebekodu" id="muhasebekodu" /></td>
                    <td>
                        <select data-bind="options: gidertipleri, value: selectedOptionValue" onchange="SetCodeField(this.value);" id="muhcodes" name="muhcodess"></select></td>

还有这样的模型

    var self = null;
    var viewModel = null;
    $(document).ready(function () {
        var giderlers = JSON.parse(document.getElementById('ContentPlaceHolder1_hdnOutgoingTypes').value);
        var selectedGider = document.getElementById('ContentPlaceHolder1_hdnOutgoingTypesSelected').value;
        var odemetipleri = JSON.parse(document.getElementById('ContentPlaceHolder1_hdnFirmPaymentTypes').value);
        var selectedOdemeTipi = document.getElementById('ContentPlaceHolder1_hdnFirmPaymentTypesSelected').value;
        var GiftModel = function (gifts) {
            self = this;
            self.gifts = ko.observableArray(gifts);

            self.addGift = function () {
                self.gifts.push({
                    muhasebekodu: ko.observable().extend({ notify: 'always' }),
                    gidertipleri: giderlers,
                    selectedOptionValue: ko.observable(selectedGider),
                    faturano: "",
                    matrah: "",
                    kdv: "",
                    geneltoplam: "",
                    kdvtipleri: ["0", "1", "8", "18"],
                    selectedOKDVptionValue: ko.observable("0"),
                    odemeseklitipleri: odemetipleri,
                    selectedOOSTptionValue: ko.observable(selectedOdemeTipi),
                    aciklama: "",
                });
            };

            self.removeGift = function (gift) {
                self.gifts.remove(gift);
            };

            self.save = function (form) {
                alert("Could now transmit to server: " + ko.utils.stringifyJson(self.gifts));
                // To actually transmit to server as a regular form post, write this: ko.utils.postJson($("form")[0], self.gifts);
            };
        };


        viewModel = new GiftModel([
            { muhasebekodu: ko.observable().extend({ notify: 'always' }), gidertipleri: giderlers, selectedOptionValue: ko.observable(selectedGider), faturano: "", matrah: "", kdv: "", geneltoplam: "", kdvtipleri: ["0", "1", "8", "18"], selectedOKDVptionValue: ko.observable("0"), odemeseklitipleri: odemetipleri, selectedOOSTptionValue: ko.observable(selectedOdemeTipi), aciklama: "" },
        ]);
        ko.applyBindings(viewModel);

        // Activate jQuery Validation
        //$("form").validate({ submitHandler: viewModel.save });


        $(".faturanoinp").bind('keyup', function (e) {
            $(this).val($(this).val().toUpperCase());
        });

    });

当用户在同一屏幕上更改下拉控件时,我想设置我的 muhasebekodu 输入,我在 jscript 下面写了。它已更改为模型中的新值,但未显示在控件中

    function SetCodeField(e) {
        var mcode = document.getElementById('ContentPlaceHolder1_hdnmuhasebekodlari').value;
        var mcodes = mcode.split(",");
        for (i = 0; i < mcodes.length; i++) {
            var namevalues = mcodes[i].split(":");
            var name = namevalues[0];
            var value = namevalues[1];
            if (e == name) {
                self.gifts()[self.gifts.length].muhasebekodu.value = value;
                self.gifts()[self.gifts.length].muhasebekodu.extend({ notify: 'always' });
            }
        }
    }

我有 2 个控件,其中一个已更改我想在另一个控件上看到始终绑定到视图模型。 self.gifts.XXX = 'new value';我想在绑定控件上看到这个

在 knockout 中,您可以像这样设置 observable 的值:

self.gifts()[self.gifts.length].muhasebekodu(value);