簡體   English   中英

在剔除Js中使用data-bind = with時,選項值為空

[英]Option values are empty when using data-bind=with in knockoutJs

我正在使用帶有剔除Js的選擇輸入下拉菜單,但是當在剔除Js中使用data-bind = with時,選項值為空。 誰可以幫我這個事。

Dropdown.html

 <span class="price"><select data-bind="options: preferedTimeToPickup,optionsCaption: 'Dont Know or Does not Matter',
    optionsText: 'name',value: preferedTimeToPickupVal" id="u3413_input" ></select>
    </span>

Custom.js

    this.preferedTimeToPickup = 
    [{name:"Morning (8-11)",price:5},
     {name:"Lunch (11-2)",price:6},
     {name:"Afternoon (2-5)",price:7},
     {name:"Specific: 8:00",price:8.5},
     {name:"Specific: 9:00",price:9.5},
     {name:"Specific: 10:00",price:10.25},
     {name:"Specific: 11:00",price:11.25},
     {name:"Specific: 12:00",price:12.25},
     {name:"Specific: 1:00",price:13.25},
     {name:"Specific: 2:00",price:14.25},
     {name:"Specific: 3:00",price:15.25},
     {name:"Specific: 4:00 (closed at 4 on sat)",price:16.25}];

     this.preferedTimeToPickupVal = ko.observable();

使用下面的html顯示數據

<p data-bind="with: preferedTimeToPickupVal">
 <span data-bind="text: name"></span> 
</p>
<p data-bind="with: preferedTimeToPickupVal">
<span data-bind="text: price"></span>
</p>

到目前為止,一切工作正常,但是在select下拉框中, value=""為空,如果我在input type select使用optionsValue: 'name' ,則值呈現良好,但data-bind="with: preferedTimeToPickupVal為無法在我想顯示數據的地方工作。

任何幫助,將不勝感激。

您的代碼工作正常,請確保正確使用this 代碼

var vm = function () {
    this.preferedTimeToPickup = [{
        name: "Morning (8-11)",
        price: 5
      }
      //...
    ]

    this.preferedTimeToPickupVal = ko.observable()
}
ko.applyBindings(new vm());

編輯

好吧,我想我明白你想要什么。 請看我更新的小提琴

基本上,如果要在select標記中為每個option節點都具有value屬性,則必須在option綁定中使用optionsValue: 'name'選項。

這樣,您現在將存儲對象的name屬性,而不是完整的對象。 現在,您必須找到一種方法來獲取數組中與所選值匹配的正確對象。

為此,您可以使用一個臨時變量來保存您的選擇,並計算一個可觀察到的變量,以在選擇更改時過濾該數組,並獲得正確的對象。

<span class="price"><select data-bind="options: preferedTimeToPickup,
    optionsCaption: 'Dont Know or Does not Matter',
    optionsValue: 'name',
    optionsText: 'name',
    value: _preferedTimeToPickupVal" id="u3413_input" ></select>
</span>

var vm = function () {
    this.preferedTimeToPickup = [ {}, {} ]
    this._preferedTimeToPickupVal = ko.observable();
    this.preferedTimeToPickupVal = ko.pureComputed(function() {
      var selectedVal = this._preferedTimeToPickupVal()
      var defaultVal = { price: null, name: null }
      var found = null
      if (selectedVal) {
        found = this.preferedTimeToPickup.filter(function(i) {
          return i.name === selectedVal
        })
      }
      return found && found[0] ? found[0] : defaultVal
    }, this)
}

您只需要添加optionsValue: price並使用pureComputed根據所選值獲取正確的信息。

 function viewModel () { var self = this; self.preferedTimeToPickup = [ {name:"Morning (8-11)",price:5}, {name:"Lunch (11-2)",price:6}, {name:"Afternoon (2-5)",price:7}, {name:"Specific: 8:00",price:8.5}, {name:"Specific: 9:00",price:9.5}, {name:"Specific: 10:00",price:10.25}, {name:"Specific: 11:00",price:11.25}, {name:"Specific: 12:00",price:12.25}, {name:"Specific: 1:00",price:13.25}, {name:"Specific: 2:00",price:14.25}, {name:"Specific: 3:00",price:15.25}, {name:"Specific: 4:00 (closed at 4 on sat)",price:16.25} ]; self.selectedPrice = ko.observable(""); // use this to get the selected value object and show it on the view self.preferedTimeToPickupVal = ko.pureComputed(function() { if(self.selectedPrice() !== "") return ko.utils.arrayFirst(self.preferedTimeToPickup, function(time) { return self.selectedPrice() === time.price; }); return null; }, this); } ko.applyBindings(new viewModel()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <span class="price"> <select data-bind="options: preferedTimeToPickup,optionsCaption: 'Dont Know or Does not Matter', optionsText: 'name', optionsValue: 'price', value: selectedPrice" id="u3413_input" ></select> </span> <p data-bind="with: preferedTimeToPickupVal"> <span data-bind="text: name"></span> </p> <p data-bind="with: preferedTimeToPickupVal"> <span data-bind="text: price"></span> </p> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM