簡體   English   中英

剔除簡單綁定問題

[英]knockout simple binding issue

這可能是最簡單的敲除綁定形式之一,

在這里找不到問題所在:

小提琴

http://jsfiddle.net/qfntcfn8/

            <input data-bind="text:newGroupName" type="text" />
            <button class="btn" type=button data-bind="click: addGroup()">
                Add Group
            </button>

ViewModel:

    var vm = $(function() {
        function baseViewModel() {
            var self = this;
            self.newGroupName = ko.observable();
            self.addGroup = function () {
                console.log(ko.toJSON(self.newGroupName)); // Expected newGroupName entered data
            };
        }
        var viewModel = new baseViewModel();
        ko.mapping.fromJS(viewModel);
        ko.applyBindings(viewModel, document.getElementById("Box"));
    });

我希望單擊后獲得newGroupName綁定文本作為字符串。

綁定輸入值時,需要使用value binding-handler:

<input data-bind="value: newGroupName" type="text" />

另外,由於newGroupName是一個可觀察的函數 ,因此您需要調用它來獲取其值:

console.log(self.newGroupName())

暫無
暫無

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

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