2015-02-05 106 views
0

我是新淘汰賽,我無法解決一個小問題,即將複選框設置爲默認選定。無法設置檢查爲默認 - 淘汰賽複選框

HTML:

<tr> 
    <td> 
     <input id="summary" type="checkbox" checked="checked" data-bind="checked: Summarise"/> 
     Summary 
    </td> 
</tr> 

Doc.Ready:

if (!viewModel) { 
    viewModel = new ViewModel(); 
} 

ko.applyBindings(viewModel, document.getElementById("ParametersView")); 

這將返回當它被調用未選中的複選框。

+0

在您的視圖模式中給出'this.Summarise(true)'。工作小提琴http://jsfiddle.net/supercool/LkqTU/22460/ – 2015-02-05 16:37:36

+0

@supercool是正確的。您的視圖不應該試圖驅動模型的默認值。擺脫'checked =「checked」'屬性。 – 2015-02-05 16:51:54

+0

謝謝guys.I現在效果很好! – spongecode 2015-02-05 17:38:57

回答

1

默認情況下,要設置複選框checked,我們只需要將true分配給綁定到checked prop的observable。

視圖模型:

var viewModel= { 
    Summarise:ko.observable(true) 
} 

ko.applyBindings(viewModel); 

查看:

<input id="summary" type="checkbox" data-bind="checked: Summarise"/> 

工作撥弄here

對於文檔,請參閱here

+0

不,這是一個完全不同的場景,還有一天的另一個問題。做它標記。感激它,歡呼 – 2015-02-06 16:18:40

0

使用ko.observable(true),然後將其綁定到輸入並檢查屬性:有關說明,請參見documentation關於淘汰網站。