问题
I have the following code
<paper-radio-group
id="marital-status-group"
on-change={{changeHandler}}>
<paper-radio-button name='Married' label='Married'></paper-radio-button><br>
<paper-radio-button name='Divorced' label='Divorced'></paper-radio-button><br>
<paper-radio-button name='Single' label='Single'></paper-radio-button><br>
<paper-radio-button name='Visiting' label='Visiting'></paper-radio-button>
</paper-radio-group>
void changeHandler( CustomEvent e )
{
print ( ( e.target as PaperRadioButton ).label );
}
When the button with label="Divorced' is clicked, the following is the result
Divorced (http://localhost:8080/packages/polymer/src/js/polymer/polymer.js:12)
All I need is the 'Divorced' NOT the other part in parenthesis. If I should click another button, then both selections are printed.
Something is wrong. What are the correct options please.
回答1:
I think your example works as expected - even as expected by you ;-) (http://localhost:8080/packages/polymer/src/js/polymer/polymer.js:12)
is only added by the print()
method.
print((e.target as PaperRadioButton).label == 'Male');
prints
false (http://localhost:8080/packages/polymer/src/js/polymer/polymer.js:12)
true (http://localhost:8080/packages/polymer/src/js/polymer/polymer.js:12)
depending on which element you select. Therefore you can just use the label value in your code.
I don't know why print()
adds this though.
In the <app-element>
which I used for this test it adds different text depending on where I print
AppElementConstructor (:1)
attached (:1)
ChangeEventhandler (http://localhost:8080/packages/polymer/src/js/polymer/polymer.js:12)
(I'm already used to (:1)
but the .../polymer.js:12
is new to me. I suppose it is some kind of zone or isolate info.
Info
The selected
attribute of the paper-radio-group
is bound to the name
attribute of the selected paper-radio-button
<paper-radio-group
id="marital-status-group"
selected="{{selectedName}}">
<paper-radio-button name='Married' label='Married'></paper-radio-button><br>
<paper-radio-button name='Divorced' label='Divorced'></paper-radio-button><br>
<paper-radio-button name='Single' label='Single'></paper-radio-button><br>
<paper-radio-button name='Visiting' label='Visiting'></paper-radio-button>
</paper-radio-group>
and in the Dart code of your element
@observable String selectedName;
void selectedNameChanged(old) {
// do something when the another radio button got selected
}
来源:https://stackoverflow.com/questions/24765923/getting-the-value-of-a-paper-radio-button