【问题标题】:bootstrap horizontal radio button group not working with knockout js引导水平单选按钮组不适用于淘汰赛js
【发布时间】:2015-10-28 11:58:37
【问题描述】:

很难让我的淘汰单选按钮绑定与引导水平单选按钮组一起使用。

这是小提琴。

http://jsfiddle.net/LkqTU/27489/

这里是代码。

 <h3>Works When I don't use the button groups</h3>

<input type="radio" name="processingType" value="Partial" data-bind="checked: ProcessingChoice" /> Partial
<input type="radio" name="processingType" value="Total" data-bind="checked: ProcessingChoice" /> Total
<input type="radio" name="processingType" value="OverUnder" data-bind="checked: ProcessingChoice" /> Over Under
<p>
    <br>The Choice is <strong><span data-bind="text: ProcessingChoice"></span></strong>

<hr>
    <h3>With Button Groups not so much</h3>

        <div class="btn-group" data-toggle="buttons">
            <label class="btn btn-default">
                <input type="radio" name="BootstrapprocessingType" value="Partial" autocomplete="off" data-bind="checked: BootstrapProcessingChoice">Partial</label>
            <label class="btn btn-default">
                <input type="radio" name="BootstrapprocessingType" value="Total" autocomplete="off" data-bind="checked: BootstrapProcessingChoice">Total</label>
             <label class="btn btn-default">
                <input type="radio" name="BootstrapprocessingType" value="Over Under" autocomplete="off" data-bind="checked: BootstrapProcessingChoice">OverUnder</label>
                </div>
                <div> <br>The Bootstrap Choice is <span data-bind="text: BootstrapProcessingChoice"></span></div>

这是视图模型

var ViewModel = function () {
    this.ProcessingChoice = ko.observable("Total");
    this.BootstrapProcessingChoice = ko.observable("Total");
};

ko.applyBindings(new ViewModel());

【问题讨论】:

标签: twitter-bootstrap knockout.js


【解决方案1】:

由于buttons.js 是一个自定义的bootstrap/jquery 插件,所以您需要一个自定义绑定。不是最好的,但会起作用

ko.bindingHandlers.bsButtonChecked = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        var val = valueAccessor();
        $(element).on('change', function(e,v,c){
           val($(element).val()) 
        });

    },
    update: function(element, valueAccessor){
        var val = valueAccessor();
        var $parent = $(element).closest('[data-toggle="buttons"]')
        var all = $parent.find("input:radio");
        $.each(all, function(index,element){
           if($(element).val()==val()){
            $(element).trigger('click');
           }
        });
    }
};

Fiddle

【讨论】:

    【解决方案2】:

    好的,谢谢,我使用其他帖子之一让它工作了,必须稍微改变一下。

    <span class="btn-group dontshowwhenloading modelLoad">
                                                    <!-- ko if: HasPartial -->
                                                   <button data-bind="css: partialButtonCss, click: function () { ProcessingChoice('Partial') }" type="button" class="btn" value="Partial">Partial</button>
                                                    <!-- /ko -->
                                                    <!-- ko if: HasTotal -->
                                                   <button data-bind="css: totallButtonCss, click: function () { ProcessingChoice('Total') }" type="button" class="btn" value="Total">Total</button>
                                                    <!-- /ko -->
                                                    <!-- ko if: HasVariance -->
                                                     <button data-bind="css: overUnderlButtonCss, click: function () { ProcessingChoice('OverUnder') }" type="button" class="btn" value="OverUnder">Over Under</button>
                                                    <!-- /ko -->
                                                </span>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2013-12-03
      • 1970-01-01
      • 2018-03-25
      • 2013-03-08
      • 2014-02-26
      相关资源
      最近更新 更多