【问题标题】:How to bind back boolean value to radio button using Aurelia如何使用 Aurelia 将布尔值绑定到单选按钮
【发布时间】:2016-12-31 05:27:05
【问题描述】:

在我的场景中,我必须将在页面加载时从数据库中获取的布尔值绑定到一组单选按钮。如果该属性的值为 true,则应选中单选按钮。组件 html 如下所示

<template>
<div repeat.for="option of question.Options" class="row">
    <template if.bind="question.QuestionTypeID == 2">
        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1" style="text-align: right;padding-right:0px;">
            <input type="radio" name="${option.QuestionID}" id="${option.OptionID}" model.bind="option.Response"  checked.bind="option.Response" click.delegate="OptionUpdate($event.target)" />
        </div>
    </template>
    <div class="col-lg-11 col-md-11 col-sm-11  col-xs-11">
        <label for="${option.OptionID}" innerhtml.bind="option.OptionDesc">
        </label>
    </div>
</div>

我在 option.Response 中绑定了布尔值 click.delegate 方法如下所示

OptionUpdate(element)
{
    for(let option in this.question.Options)
    {
        if(element.id == this.question.Options[option].OptionID && element.checked)
        {
            this.question.Options[option].Response = this.question.Options[option].OptionID;
        }
        else{
            this.question.Options[option].Response = undefined;
        }
    }
    return element.checked;
}

即使 option.Response 属性为 true,也会检查其他单选按钮。

提前致谢

【问题讨论】:

  • 你能解决这个问题吗?

标签: javascript aurelia


【解决方案1】:

您不需要OptionUpdate。这要简单得多。我创建了一个 GistRun 来帮助您使用来自数据库的数据对象的单选框:

https://gist.run/?id=de0a9a277ddbaf6ff39331f3fc61cfe1

【讨论】:

  • 您可以在 Aurelia 中使用嵌套的
  • 嵌套模板非常好
  • 谢谢。我更新了我的答案以删除有关嵌套模板标签的建议。
猜你喜欢
  • 2013-05-01
  • 2023-03-18
  • 2011-02-16
  • 1970-01-01
  • 2015-04-27
  • 2013-06-02
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多