2017-05-16 69 views
0

爲什麼我可以同時選擇兩個單選按鈕?爲什麼我可以同時選擇兩個單選按鈕?

<form #form="ngForm"> 
    {{ poll.counter1 }} votes <input type="radio" id="{{ poll.choice1 }}" value="{{ poll.choice1 }}" (click)="onChoice1(form)">{{ poll.choice1 }} 
    <br> 
    {{ poll.counter2 }} votes <input type="radio" id="{{ poll.choice2 }}" value="{{ poll.choice2 }}" (click)="onChoice2(form)">{{ poll.choice2 }} 
    </form> 

回答

3

如果你只想選擇一個,你必須給這兩個單選按鈕同名。

<form #form="ngForm"> 
    {{ poll.counter1 }} votes <input type="radio" name="my_radio" id="{{ poll.choice1 }}" value="{{ poll.choice1 }}" (click)="onChoice1(form)">{{ poll.choice1 }}   
    {{ poll.counter2 }} votes <input type="radio" name="my_radio" id="{{ poll.choice2 }}" value="{{ poll.choice2 }}" (click)="onChoice2(form)">{{ poll.choice2 }} 
</form> 

你可以在這裏嘗試一下:

<label>Radio A</label> 
 
<input type="radio" name="foo"> 
 
<label>Radio B</label> 
 
<input type="radio" name="foo"> 
 

 
<h2>Without the same name</h2> 
 
<label>Radio X</label> 
 
<input type="radio"> 
 
<label>Radio Y</label> 
 
<input type="radio">

相關問題