KnockoutJS - bootstrap 3模态绑定不起作用

时间:2013-11-14 18:53:42

标签: javascript twitter-bootstrap knockout.js

我有一个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打开这个模态。

好的,如果我将username属性设置为某个值...

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

...值正确显示在外部那个模式div ...

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

...但不在上面的模式div中的那个文本框上工作。

任何人都知道发生了什么事?

谢谢!

1 个答案:

答案 0 :(得分:0)

正如其他人已经提到的那样,发布更完整版本的代码会让其他人帮助您。如果不发布您编写的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());