简体   繁体   English

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

[英]KnockoutJS - bootstrap 3 modal binding doesn't working

I have a Bootstrap modal div: 我有一个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>

I'm opening this modal through JS. 我正在通过JS打开此模式。

Ok, if I set the username property with some value... 好的,如果我将username属性设置为某个值...

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

... the value is properly displayed on a span element outside that modal div... ...该值正确显示在该模态div 之外的span元素上...

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

... but isn't working on that textbox in the modal div above. ...但无法在上面的模式div中的该文本框上使用。

Anyone knows what's happening? 有人知道发生了什么吗?

Thanks! 谢谢!

As others have already mentioned, posting a more complete version of your code would go a long ways in enabling others to help you. 正如其他人已经提到的那样,发布更完整的代码版本将大大有助于其他人为您提供帮助。 Without posting the javascript you have written, people can only make guesses at what you are doing. 在不发布您编写的JavaScript的情况下,人们只能猜测您在做什么。

That being said, here is an example of your modal where knockout does display the username inside the modal as well as outside the modal. 话虽如此,这是您的模态示例,其中剔除确实在模态内部以及模态外部都显示了用户名

http://jsbin.com/AFORuzAT/2/edit 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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM