2017-04-22 48 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/