最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Aurelia radio button binding - Stack Overflow

programmeradmin0浏览0评论

Hope someone can help me with this. I've got the following view:

<label repeat.for="option of options">
    <input type="radio" name="periodOptions" model.bind="option" checked.bind="$parent.selectedOption" click.delegate="clicked()"/>
    ${option.text}
</label>

and the following viewmodel:

export class PeriodPanel {
    heading = 'Tidsperiode';
    options = [];
    selectedOption = {};

    constructor() {
        this.options = [
            {id:1, text:'Vis med dagoppløsning'}, 
            {id:2, text:'Vis med timeoppløsning'}, 
            {id:3, text:'Vis periode'}
        ]; 
        this.selectedOption = this.options[0];
    }

    clicked() {
        console.log(this.selectedOption.id);
    }
}

The reason for the assignment this.selectedOption = this.options[0]; is to make sure that one of the radio buttons are initially set. This is all nice and dandy, but when I click on each radio button in turn, the value of the selectedOption variable does not change and the click-handler clicked() will print the value 1 each time. What am I doing wrong?

TIA

Hope someone can help me with this. I've got the following view:

<label repeat.for="option of options">
    <input type="radio" name="periodOptions" model.bind="option" checked.bind="$parent.selectedOption" click.delegate="clicked()"/>
    ${option.text}
</label>

and the following viewmodel:

export class PeriodPanel {
    heading = 'Tidsperiode';
    options = [];
    selectedOption = {};

    constructor() {
        this.options = [
            {id:1, text:'Vis med dagoppløsning'}, 
            {id:2, text:'Vis med timeoppløsning'}, 
            {id:3, text:'Vis periode'}
        ]; 
        this.selectedOption = this.options[0];
    }

    clicked() {
        console.log(this.selectedOption.id);
    }
}

The reason for the assignment this.selectedOption = this.options[0]; is to make sure that one of the radio buttons are initially set. This is all nice and dandy, but when I click on each radio button in turn, the value of the selectedOption variable does not change and the click-handler clicked() will print the value 1 each time. What am I doing wrong?

TIA

Share Improve this question edited Oct 12, 2016 at 22:44 Jeremy Danyow 26.4k12 gold badges90 silver badges135 bronze badges asked Feb 2, 2016 at 18:10 norgienorgie 5177 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Return true from your clicked() method to allow the event to propagate.

Here's a working example:

https://gist.run/?id=84eb0949ff63c3f10a1eff3c337f2c97

发布评论

评论列表(0)

  1. 暂无评论