7

我有一个复选框和一个单选按钮组,我想知道是否选中了复选框以及选择了哪个单选按钮。

我如何在飞镖中做到这一点?

4

2 回答 2

8

假设我们有这样的 HTML:

    <form >
      <input type="radio" name="gender" id="gender_male" value="male">Male<br>
      <input type="radio" name="gender" id="gender_female" value="female">Female
    </form>

    <form>
      <input type="checkbox" id="baconLover">I like bacon<br>
    </form>

获取它们的值的 Dart 代码如下所示,我还添加了一个事件来了解何时单击复选框。

import 'dart:html';

void main() {

  // Adds a click event when the checkbox is clicked
  query("#baconLover").on.click.add((MouseEvent evt) {
    InputElement baconCheckbox = evt.target;

    if (baconCheckbox.checked) {
      print("The user likes bacon");
    } else {
      print("The user does not like bacon");
    }

  });

  // Adds a click event for each radio button in the group with name "gender"
  queryAll('[name="gender"]').forEach((InputElement radioButton) {
    radioButton.onclick.listen((e) {
      InputElement clicked = e.target;
      print("The user is ${clicked.value}");
    });
  });

}
于 2012-11-01T17:03:36.883 回答
1

我找到了这个单选按钮的解决方案,其中通过“html”捕获事件......我已经在我的项目中使用了这个解决方案。

my_example.html

<polymer-element name="my-example">
  <template>
    <div on-change="{{updateRadios}}">
      Your favorite color is:
      <div>
        <label for="red">Red <input name="color" type="radio" id="red" value="red"></label>
      </div>
      <div>
        <label for="green">Green <input name="color" type="radio" id="green" value="green"></label>
      </div>
      <div>
        <label for="blue">Blue <input name="color" type="radio" id="blue" value="blue"></label>
      </div>
    </div>
    <div>
      You selected {{favoriteColor}}
    </div>
  </template>
  <script type="application/dart" src="my_example.dart"></script>
</polymer-element>

my_example.dart

import 'package:polymer/polymer.dart';
import 'dart:html';

@CustomTag('my-example')
class MyExample extends PolymerElement {
  @observable String favoriteColor = '';

  MyExample.created() : super.created();

  void updateRadios(Event e, var detail, Node target) {
    favoriteColor = (e.target as InputElement).value;
  }

}
于 2014-08-31T10:48:32.443 回答