【问题标题】:Knockout radio checked binding淘汰赛无线电检查绑定
【发布时间】:2017-08-30 07:04:01
【问题描述】:

我有一些radio buttons 使用剔除检查绑定。使用收音机进行初始提交后,模板获取收音机的值,正确的radio buttonchecked。问题是当我重新加载之前提交的页面时不再有任何checked radios

代码如下:

<input data-bind= "checked: Type, required:Type type="radio" value="1" />
<label>Allowed</label>
<input data-bind= "checked: Type, required: Type, type="radio" value="2" />
<label>Charge</label>

在使用 Chrome 查看淘汰赛上下文插件时,它显示类型设置为原始提交值,但是在重新加载时仍然没有检查它们。

【问题讨论】:

    标签: javascript html knockout.js


    【解决方案1】:

    如果您想在页面重新加载时保留价值,您可能需要使用本地存储。

    这是一个小提琴,当您重新加载页面时,它将保留您上次选中的单选按钮。

    https://jsfiddle.net/0o89pmju/50/

    html

    <input type="radio" data-bind="checked: Type"  value="1" name="myradio" />
    <label>Allowed</label>
    <input type="radio" data-bind="checked: Type"  value="2" name="myradio" />
    <label>Charge</label>
    
    <p>
      the value is: <label data-bind="text: Type"></label>
    </p>
    

    javascript

    function Model() {
      var self = this;
      this.Type = ko.observable(localStorage.getItem("Type"))
    }
    var vm = new Model()
    $( document ).ready(function() {
        ko.applyBindings(vm);
        vm.Type.subscribe(function(newValue) {
           localStorage.setItem("Type", newValue);
       });
    });
    

    【讨论】:

      猜你喜欢
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2013-12-27
      • 2013-08-06
      • 2013-01-01
      • 2013-10-31
      相关资源
      最近更新 更多