Getting the value of a paper-radio-button

ぃ、小莉子 提交于 2019-12-10 16:54:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!