javascript - 带有图像的样式 knockout.js 单选按钮

标签 javascript css knockout.js

我正在使用 knockout.js 在用户选择单选按钮时显示价格和 ID,这里是脚本:

<div data-bind="with: bin2ViewModel">
    <div class="divRadiobtns" data-bind="foreach: availableGroups">
    <input type="radio" class="radioOptions" name="retailerGroup" data-bind="checked: $parent.selectedGroupOption, value: price" />
    </div>
    <div data-bind="with: selectedRetailerGroup">
    <span class="actualPrice" data-bind="text: price" />
    </div>
    <div data-bind="with: selectedRetailerGroup">
    <input type="hidden"  class="hiddenValue" data-bind="value: retailerproductId" />
    </div>
    </div>

knockout .js:

<script type="text/javascript">
//<![CDATA[
     var Bin2ViewModel = function () {
     var self = this;
     this.selectedRetailerGroup = ko.observable();
     this.selectedGroupOption = ko.observable();
     this.selectedGroupOption.subscribe(function (newVal) {
     var items = $.grep(self.availableGroups(), function (item) { return item.price() == newVal; });
     self.selectedRetailerGroup(items[0]);
     });
     this.selectedGroup = ko.observable();
     this.availableGroups = ko.observableArray(
    [  new RetailerViewModel("302852", "£2.55"),
  new RetailerViewModel("21290", "£1.80")
     ]);
     }
     var RetailerViewModel = function (retailerproductId, price) {
     this.retailerproductId = ko.observable(retailerproductId);
     this.price = ko.observable(price);
     }
     ko.applyBindings({ bin2ViewModel: ko.observable(new Bin2ViewModel()) });
//]]>
 </script>

我想用图像设置单选按钮的样式,所以我尝试过:

CSS:

 .radioOptions
        {
            background: url("http://static.e-talemedia.net/content/images/odditiesyellowselector.png") no-repeat scroll 0 0 transparent;
           margin-bottom: 25px;
    margin-top: 17px;
}

.radioOptions-checked
    {
        background: url("http://static.e-talemedia.net/content/images/odditiesyellowblackselector.png") no-repeat scroll 0 0 transparent;
    }

但我无法正常工作 - 有没有人知道我如何在 knockout.js 中设置样式?

我以前使用 jquery 如下:

<%--<script type="text/javascript">
      //<![CDATA[
        $(function () {
            $('input:radio').hide().each(function () {
                var label = $("label[for=" + '"' + this.id + '"' + "]").text();
                $('<a title=" ' + label + ' " class="radio-fx ' + this.name + '" href="#"><span class="radio"></span></a>').insertAfter(this);

            });

            $('.radio-fx').click(function () {
                $check = $(this).prev('input:radio');
                var unique = '.' + this.className.split(' ')[1] + ' span';
                $(unique).attr('class', 'radio');
                $(this).find('span').attr('class', 'radio-checked');
                $check.attr('checked', true);
                var rpPrice = $('input[name=selectRetailer]:radio:checked');
                // Here I have an ID
                $(".actualPrice").html(rpPrice.val());

                //                var rpId = $('input[name=selectRetailer]:radio:checked');
                //                $(".actualPrice").html(rpId.val());

            }).on('keydown', function (e) {
                if (e.which == 32) {
                    $(this).trigger('click');
                }

            });


        });      
    //]]>
    </script>--%>

但这抛出了 knockout.js

感谢任何提示!

最佳答案

您不能直接设置输入元素的背景样式,但这将为您提供一个起点:http://jsfiddle.net/7aPwp/2

您应该为样式使用 style 绑定(bind),为类使用 css

例子:

<span class="radioOptions" data-bind="css: { 'radioOptions-checked': $parent.selectedGroupOption() == price() }">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>

关于javascript - 带有图像的样式 knockout.js 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14255521/

相关文章:

javascript - React native - 安装 native-base 后版本控制中包含哪些文件?

javascript - ASP.net MVC - View 和 jQuery 最佳实践

html - css 淡出边缘的框阴影

javascript - 在 Knockout 中“取消销毁”一个对象

javascript - 将嵌套的 knockout View 模型传递给 Controller

javascript - 如何修复我的元素中未加载图像和 CSS?

javascript - 使用 ajax、php 和 mysql 进行表单验证

jquery - 具有多个部分的 1 个页面布局的可调整大小的背景

html - Img 的最大高度不尊重 parent 的尺寸

javascript - MIX 11 knockout 代码示例和我的错误