如何知道在Dart中是否选中了复选框或单选按钮? [英] How to know if a checkbox or radio button is checked in Dart?

查看:192
本文介绍了如何知道在Dart中是否选中了复选框或单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a checkbox and a radio button group and I want to know if the checkbox is checked and which radio button is selected.

如何做dart?

推荐答案

假设我们有你的HTML代码:

Let's say we have your HTML with something like this:

    <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代码获取它们的值将类似于以下内容,我还添加了一个事件

Your Dart code to obtain their values would be something like the following, I also added an event to know when the checkbox is clicked.

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

}

这篇关于如何知道在Dart中是否选中了复选框或单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆