2011-08-29 147 views
1

如何使用knockout.js禁用單選按鈕?我不希望用戶能夠選擇它。我的模板下面不起作用。使用knockout.js禁用單選按鈕

<div id="answers" data-bind="template: { name: 'answerTmpl', data: interactive.answers }"> 
    </div> 

    <script id="answerTmpl" type="text/html"> 

     {{each(index, value) $data}} 
     <div> 
      <input type="radio" value="${index}" name="Answer" data-bind="disable: app.viewModel.avatars.speaking, checked: app.viewModel.interactive.answerSelected"/> <span>${value}</span> 
     </div> 
     {{/each}} 
    </script> 

app.avatars.js

(function (app, $, undefined) { 

    app.viewModel = app.viewModel || {}; 

    app.viewModel.avatars = { 
     speaking: ko.observable(false), 
     loaded: ko.observable(false) 
    }; 

當您使用全局變量,而不是綁定數據app.interactive.js

(function (app, $, undefined) { 

    app.viewModel = app.viewModel || {}; 

    app.viewModel.interactive = { 
     timeout: 1500, 
     answers: ko.observableArray(), 
     answerSelected: ko.observable(''), 
     correctAnswer: ko.observable(-1), 
     bookPage: ko.observable(1), 
     chapterEmail: ko.observable(''), 
     trialogue: { 
      inProgress: ko.observable(false), 
      response: ko.observable(''), 
      conversation: ko.observableArray() 
     } 
    }; 

    app.interactive.init = function() { 
     ko.applyBindings(app.viewModel); 
    }; 
+0

只需使用avatars.speaking或avatars.speaking()也可以在這裏看看http://knockoutjs.com/documentation/enable-binding.html – Aviatrix

+0

這適用於我:http://jsfiddle.net/E5aqu/你能簡化你的例子,幷包含更多的相關代碼嗎? – Douglas

+0

我正在使用jQuery模板。我的視圖模型是由兩個不同的js文件組成的。 –

回答

2

,它看起來像你需要明確評估功能:http://jsfiddle.net/nFgnj/

<input type="radio" value="${index}" name="Answer" 
    data-bind="disable:app.viewModel.avatars.speaking(), 
       checked:app.viewModel.interactive.answerSelected()" /> 

我不太喜歡這個解決方案,因爲它依賴於作爲全局變量的app