2014-04-04 51 views
0

我有兩個選擇,第二個取決於第一個 我想使用敲除檢查,如果第一個選定的值改變,調用一個函數,用ajax調用填充第二個選擇。 第一選擇填入服務器端當選定的值改變與敲除時的呼叫功能

$(document).ready(function() { 
     function FilterModel() { 
      this.Datevalue = ko.observable(); 

      this.addSecond = function() { 
        --- 
      } 

     } 

     $(function() { 
      ko.applyBindings(new FilterModel()); 
     }); 
    }); 

HTML

<select id="fisrt" data-bind="value: Datevalue" ></select> 
<select id="second" ></select> 

我如何可以調用addSecond?

回答

1

您可以使用一個計算的布爾驗證Datevalue並使用visible:結合顯示第二<select>

http://jsfiddle.net/t8gxv/

喚起addSecond您可以訂閱可觀察並調用addSecond當值的變化:

http://jsfiddle.net/t8gxv/1/

$(document).ready(function() { 
    function FilterModel() { 
     var self = this; 

     this.Datevalue = ko.observable(); 
     this.addSecond = function() { 
       alert('addSecond'); 
     } 

     // boolean for showing the second select 
     this.showSecond = ko.computed(function() { 
      return this.Datevalue() == 'bar'; 
     }, this); 

     // calling addSecond when the correct value is selected 
     this.Datevalue.subscribe(function(newValue) { 
      if(newValue == 'bar') { 
       self.addSecond(); 
      } 
     }); 

     // easier even is to subscribe to the computed 
     var subscription = this.showSecond.subscribe(function(newValue) { 
      // if true 
      if(newValue) { 
       self.addSecond(); 

       //only want to call addSecond once 
       subscription.dispose(); 
      } 
     }); 

    } 

    $(function() { 
     ko.applyBindings(new FilterModel()); 
    }); 
}); 

也可以訂閱計算和處置訂閱所以addSecond只會被調用一次: http://jsfiddle.net/t8gxv/2/

0
JAVASCRIPT: 
this.Datevalue = ko.observable(); 
this.MakeSecondVisible = ko.observable(); 
this.ondropDownChange= function() { 
     if(this.Datevalue() == something) 
     { 
      this.MakeSecondVisible = true; 
     } 


HTML : 

<select data-bind="value: Datevalue" onclick: ondropDownChange() > //FIRST ONE 

<select data-bind="options: optionValues" visible: MakeSecondVisible > //SECOND ONE