2017-04-22 73 views
1

我想使用Aurelia检索所选单选按钮的值。以下是我的代码。Aurelia将值绑定到支票更改上的单选按钮

<input id="account-location-1" name="account-location" type="radio" 
value.bind="Item1" checked checked.bind="accountLocation" 
click.delegate="toggle()"> 
<label for="account-location-1">Australian</label> 

<input id="account-location-2" name="account-location" type="radio" 
value.bind="Item2" checked.bind="accountLocation" click.delegate="toggle()"> 
<label for="account-location-2">International</label> 

export class TestApp{ 
accountLocation = ''; 
private toggle() { 
    alert(accountLocation); 
    return true; 
} 
} 

问题是accountLocation没有选中,但始终是一个空字符串。任何指向什么是错的?

回答

1

只需删除值.bind,它应该不再是空的:

<... 
value="Item1" 
...> 
<label for="account-location-1">Australian</label> 

<... 
value="Item2" 
...> 
<label for="account-location-2">International</label> 
+0

谢谢,完美的作品。所以,基本上它不起作用,因为我绑定了字符串值而不是表达式? –

+1

你试图将单选按钮的值绑定到一个不存在的变量,这就是为什么你得到一个空的结果。要看看它是如何工作的,你可以使用你的旧代码并创建一个变量:'Item2 =“example”;'然后当你按下按钮时你的旧代码将打印“example”。 – greensponge

0

我想附和这个 - 我今天这个发现:该value.bind锁定值到accountLocation值。

你可能想要做的是一个model.bind它也可以自动设置默认值,该列表应该来自一个数组。

我对这个位置(即有链接与工作示例GIST)一点点写了,因为我是有越来越RADIO输入,显示预先选定的价值问题:

http://codehot.io/aurelia-model-bind-vs-value-bind/