如何将Jquery对话框按钮绑定到淘汰视图模型 [英] How to bind Jquery dialog buttons to a knockout viewmodel

查看:107
本文介绍了如何将Jquery对话框按钮绑定到淘汰视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是创建一个对话框,其中按钮与数据绑定到剔除视图模型,因此我可以根据表单上的各种条件启用或禁用这些按钮

What I'd like to do is make a dialog where the buttons are databound to the knockout viewmodel so I can enable or disable those buttons depending on various conditions on the form

但是在jquery对话框中制作按钮的方式与正常情况有所不同.

But the way you make buttons in jquery dialogs is a bit different than normal.

有人对此有解决方案吗?

anyone have a solution for this?

推荐答案

  1. 确保将自己的class应用于对话框的按钮:

  1. Make sure to apply your own class to the dialog's buttons:

$("#dialog").dialog({
    buttons: [{
        text: 'Ok',
        class: 'ok-button'
    }]
});

  • 抓住button.ok-button并为其应用一个data-bind属性(此处为visible,只是为了向您展示它的工作原理).在这里,name是我们的视图模型的可观察属性:

  • Grab the button.ok-button and apply a data-bind attribute to it (visible here, just to show you that it works). Here, name is an observable property of our view model:

    $("button.ok-button").attr("data-bind", "visible: name().length");
    

  • 正常应用绑定:

  • Apply bindings normally:

    var model = { name: ko.observable('') };
    ko.applyBindings(model);
    

  • 这是一个示例,如果name(可观察的)的长度为> 0,则在对话框上隐藏一个确定"按钮:

    Here's an example that hide's an "Ok" button on the dialog if name (an observable) has a length > 0: http://jsfiddle.net/9cRFy/

    这篇关于如何将Jquery对话框按钮绑定到淘汰视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆