KnockoutJS - 引导程序 3 模态绑定不起作用



我有一个 Bootstrap 模态div:

<div class="modal fade" id="modalLoginData" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Login Data</h4>
        </div>
        <div class="modal-body">
            <div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <label class="sr-only">Username</label>
                <input type="text" class="form-control" placeholder="Username" data-bind="value: username" />
            </div>   
            <div class="form-group col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <label class="sr-only">Password</label>
                <input type="password" class="form-control" placeholder="Password" data-bind="value: password"  />
            </div>   
            <div class="form-group col-lg-6 col-md-6 col-sm-12 col-xs-12">
                <label class="sr-only">Repeat your password</label>
                <input type="password" class="form-control" placeholder="Repeat your password" data-bind="value: repeatedPassword" />
            </div>   
            <div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <label class="sr-only">Login</label>
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-success btn-sm" data-bind="click: confirmLoginData">Confirm</button>
            <button type="button" class="btn btn-default btn-sm" data-dismiss="modal" data-bind="click: cancelLoginData">Cancel</button>
        </div>
    </div>
</div>

我正在通过 JS 打开这个模式。

好的,如果我用一些值设置用户名属性......

function Model() {
    this.username = ko.observable("kiwanax");
}

。该值正确显示在该模态div 之外的 span 元素上。

<span data-bind="text: username"></span>

。但不适用于上述模态div 中的文本框。

有人知道发生了什么吗?

谢谢!

正如其他人已经提到的,发布更完整的代码版本将大大有助于其他人帮助你。如果不发布你编写的javascript,人们只能猜测你在做什么。

话虽如此,这里有一个模态示例,其中 knockout 确实在模态内部和模态外显示用户名

http://jsbin.com/AFORuzAT/2/edit

$('#modalLoginData').modal();
function Model() {
    this.username = ko.observable("kiwanax");
    this.password = ko.observable();
    this.repeatedPassword = ko.observable();
    this.cancelLoginData = function () {
        console.log( 'cancel!' );
    };
    this.confirmLoginData = function () {
         console.log( 'confirm login data!' );
    };
}
ko.applyBindings(new Model());

最新更新