无法从更改事件中的选择列表中获取值

时间:2014-04-16 15:53:43

标签: jquery knockout.js durandal single-page-application knockout-2.0

我有一系列选择列表(下拉框),其中我使用敲门事件绑定来触发更改事件。我有一个函数,当我从下拉列表中选择一个不同的选项时,我试图获取值,但是它始终返回我的observablearray中的第一个值。这是我正在做的事情 -

以下是我视图中的选择列表:

  <table style="width: 100%">
     <tbody data-bind="foreach: LoanDetails">
 <tr style="border: none">
     <td>
          <select id="ddlLCLoans" tabindex="4" style="width: 120px" data-bind=" optionsCaption: 'Choose...', options: $data.FilteredLCLoans, 
optionsValue: 'LoanNum', optionsText: 'LoanNumCurrency', validationOptions: { insertMessages: false }, event: { change: $parent.LCLoanSelectionChanged }, value: $data.LCLoan, disable: $parent.disableButtons">
           </select>
      </td>
  </tr>

在我的viewmodel中,我有以下变量用于生成observables:

         var buildRow = function (LCLoans) {
        var self = this;
        var obj = {               
            LCLoan: ko.observable(LCLoan).extend({ insertMessages: false, messagesOnModified: false, required: { onlyIf: validate, message: ' <BR /> LC Loans is Required' } }),

            FilteredABLoans: ko.observableArray(ABLoans())
        };

            // Add to object after obj is created so we can use that instance
            // using "this" to read the items DocStandby value
            obj.FilteredLCLoans = ko.computed(function () {

                return LCLoans().filter(function (item) {
                    return item.LoanSubType() === obj.DocStandby();
                });
            }, obj);

        obj.errors = ko.validation.group(obj);
        return obj;
    };

视图中的选择列表使用filtersCLalo(LoanNum)和optionsText(LoanNumCurrency)的filteredLCLoans observable中的字段。以下是填充filteredLCLoans的模型:

         var ClientsModel = function (clientID, LoanNum, LoanSubType, LoanNumCurrency, CurrType) {
        var self = this;

        self.clientID = ko.observable(clientID);
        self.LoanNum = ko.observable(LoanNum);
        self.LoanSubType = ko.observable(LoanSubType);
        self.LoanNumCurrency = ko.observable(LoanNumCurrency);
        self.CurrType = ko.observable(CurrType);
    };

选择列表还引用了名为LCLoanSelectionChanged的函数。当用户从选择列表中选择其他选项时,我正在尝试获取新选择的值。这是函数:

 var vm = {
        activate: activate,
        LCLoanSelectionChanged: function (row) {

            var newValue = row.FilteredLCLoans.LoanNum;
        }
  };

    return vm;

如何获取新选择的值?如果我放置一个断点并使用jQuery(var newValue = $(&#39; #ddlLCLoans选项:selected&#39;)。val())我总是在视图中获得ddlLCLoans的第一个实例值。我知道&#34; var newValue = row.FilteredLCLoans.LoanNum&#34;上面不起作用,只是不知道如何正确编码。

1 个答案:

答案 0 :(得分:0)

订阅肯定是要走的路。这是代码 -

                    var currtype = '';
                    ko.utils.arrayForEach(LoanDetails(), function (itemLoan) {
                        itemLoan.LCLoan.subscribe(function (newLoan) {
                           alert(newLoan);
                        });
                 });

将它与我上面视图中的html进行比较,您可以看到这些非常简化的事情。

谢谢Patrick Steele!