单击单选按钮时,元素内容将更改为图像

时间:2013-05-22 10:32:55

标签: javascript jquery html knockout.js knockout-mapping-plugin

我可以让它适用于文本,但无法启用它,因此它会读取图像的HTML - http://jsfiddle.net/BgQPF/

<p> Current Radiobutton Value = <span data-bind='text: selected' ></span></p>

<input type='radio' name='onlyOne' data-bind='checked: selected, enable: enable' value='<img src="https://github.com/gentoo90/minimal-django-file-upload-example/diff_blob/9ab39c7fe5abaf00f2d586e88e79a5a85075179e/src/for_django_1-3/myproject/media/documents/2012/07/12/example02.jpg?raw=true">' /> Yes
<input type='radio' name='onlyOne' data-bind='checked: selected, enable: enable' value='N' /> No
<input type='radio' name='onlyOne' data-bind='checked: selected, enable: enable' value='M' /> Maybe

2 个答案:

答案 0 :(得分:7)

这是交易

<span data-bind='html: selected' ></span>

而不是

<span data-bind='text: selected' ></span>

<强> LIVE DEMO

答案 1 :(得分:0)

你想把它输出到哪里?

在你给出的例子中,我按下第一个单选按钮时得到了正确的值,但是为什么不将href仅存储在值上并更改img元素的src attr,而不是单选按钮?

Html部分:

<p> Current Radiobutton Value = <span data-bind='text: selected' ></span></p>

<img id="test_img" src='' alt="" width="32" height="32" />

<input type='radio' name='onlyOne' data-bind='checked: selected, enable: enable' value='http://www.gravatar.com/avatar/a118c92a699e7d003b8767a108d50f53?s=32&amp;d=identicon&amp;r=PG' /> Yes
<input type='radio' name='onlyOne' data-bind='checked: selected, enable: enable' value='N' /> No
<input type='radio' name='onlyOne' data-bind='checked: selected, enable: enable' value='M' /> Maybe

<BR/>

<input type='checkbox' data-bind='checked: enable'> Enable controls

脚本部分:

var viewModel = {
    enable: ko.observable(true),
    selected: ko.observable('Y')
};


$('input[name="onlyOne"]').click(function (event) {
   $('#test_img').attr('src',$(this).val());
});

ko.applyBindings(viewModel);

使用链接示例,粗略编辑。

此致