4

我有以下代码

      <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 );
  }

当点击label="Divorced'的按钮时,结果如下

Divorced (http://localhost:8080/packages/polymer/src/js/polymer/polymer.js:12) 

我需要的只是“离婚”而不是括号中的另一部分。如果我应该单击另一个按钮,则会打印两个选择。

出了点问题。请问什么是正确的选项。

4

1 回答 1

2

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
  }
于 2014-07-18T11:03:54.383 回答