2013-02-21 78 views
1

我想設置一個敲除的observable,將禁用輸入,如果兩個其他輸入不是都在1和30之間。現在,當我在jsFiddle中運行代碼時,我的輸入被禁用。不幸的是,我永遠無法重新啓用輸入。這裏是jsFiddle上的代碼感謝您的任何幫助。禁用基於其他輸入的輸入Knockoutjs

HTML

<!-- This is a *view* - HTML markup that defines the appearance of your 
UI --> 
<p>Alcohol Days: 
    <input data-bind="value: alcoholDays" /> 
</p> 
<p>Alcohol 5+ Days: 
    <input data-bind="value: alcoholFivePlusDays" /> 
</p> 
<p>Alcohol 4- Days: 
    <input data-bind="value: alcoholFourLessDays" /> 
</p> 
<p>Drug Days: 
    <input data-bind="value: drugDays" /> 
</p> 
<p>Both Days: 
    <input data-bind="value: bothDays, enable: enableBothDays" /> 
</p> 
<p>Enable Both Days: <strong data-bind="text: enableBothDays"></strong> 

</p> 
<p>Alcohol Days: <strong data-bind="text: alcoholDays"></strong> 

</p> 
<p>Drug Days: <strong data-bind="text: drugDays"></strong> 

</p> 
<button data-bind="click: capitalizeLastName">Go caps</button> 

的JavaScript

// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI 
function AppViewModel() { 

    var self = this; 

    self.alcoholDays = ko.observable(""); 
    self.alcoholFivePlusDays = ko.observable(""); 
    self.alcoholFourLessDays = ko.observable(""); 
    self.drugDays = ko.observable(""); 
    self.bothDays = ko.observable(""); 

    self.enableBothDays = ko.computed(function() { 

     if ((parseInt(self.alcoholDays) > 0 && parseInt(self.alcoholDays) <= 30) && (parseInt(self.drugDays) > 0 && parseInt(self.drugDays) <= 30)) { 
      return true; 
     } else { 
      return false; 
     } 

    }, self); 

} 

// Activates knockout.js 
ko.applyBindings(new AppViewModel()); 

韋德

回答

1

hi check this fiddle我修復了問題

1)清除了您提琴綁定錯誤

2)重新計算結構觀察到

self.enableBothDays = ko.computed({ 
    read: function() { 
     alert('In'); 
     var alcDays = Number(self.alcoholDays()); 
     var drgDays = Number(self.drugDays()); 
     alert(alcDays+','+drgDays); 
     var temp = false; 
    if (alcDays > 0 && alcDays <= 30 && drgDays > 0 && drgDays <= 30) { 
     temp = true; 
    } else { 
     temp = false; 
    } 
     alert(temp); 
     return temp; 
} 
}); 

3)改變啓用條件

fiddle..

痕作爲回答

0

使用此 啓用:enableBothDays()==真 或者 啓用:enableBothDays ==真

1

固定小提琴 http://jsfiddle.net/SNv6n/20/

你分別致電self.alcoholDays代替self.alcoholDays()。如果您將括號添加到您的計算中的這些調用中,並添加了函數「capitalizeLastName」,它就可以工作。

self.capitalizeLastName = function() { 
    alert('TODO'); 
} 
self.enableBothDays = ko.computed(function() { 

    if ((parseInt(self.alcoholDays()) > 0 && parseInt(self.alcoholDays()) <= 30) && (parseInt(self.drugDays()) > 0 && parseInt(self.drugDays()) <= 30)) { 
     return true; 
    } else { 
     return false; 
    } 

}, self); 
+0

謝謝布萊恩,不幸的是我只能接受一個答案,並與誰先回答。 – Wade73 2013-02-21 11:10:59