radio-button 相关问题

单选按钮是表单中使用的元素。它们让用户只选择有限数量的选项中的一个。

使用 3 个选项按钮 - 启动和变量定义过程中出现问题

我的目标:使用单选按钮或选项按钮选择预定义的单元格,并使用字母/数字指示符(在本例中为“N1”)填充它们,使用 If-Then-Else,允许用户选择差异...

回答 2 投票 0

AHK 阻止单选按钮执行操作

美好的一天, 我正在尝试为 AutoHotKey (AHK) 创建脚本。脚本本身已经可以正常工作,但我的问题是,在 GUI 中,当我按下单选按钮或复选框(“颜色”和“选项”Gr...

回答 1 投票 0

获取组中选中的单选按钮的索引

我有几个单选按钮在一个组中,有没有办法获取当前选中项的索引? 现在我使用这段代码: int getCheckedRadioButton(QWidget *w) { 整数ii = 0; foreach (

回答 1 投票 0

在 .Net MAUI 中设置单选按钮样式的问题

我在 .Net MAUI 中设置单选按钮样式时遇到一些问题。最初,我注意到 Windows 和 Android 上的单选按钮外观并不一致,如 i...

回答 2 投票 0

检索数据时单选按钮的绑定值

我正在尝试检索我的 MAUI c# 桌面应用程序中“isConforme”单选按钮的值。 在我看来,该值没有正确绑定。 这是我的代码 看法: 我正在尝试检索我的 MAUI c# 桌面应用程序中“isConforme”单选按钮的值。 在我看来,该值没有正确绑定。 这是我的代码 查看: <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" xmlns:local="clr-namespace:FntrAudit.ViewModel" x:DataType="local:NewAuditViewModel" xmlns:model="clr-namespace:FntrAudit.Models" x:Class="FntrAudit.Views.NewAudit" Title="Nouvel audit" > <HorizontalStackLayout> <CollectionView ItemsSource="{Binding Audit.Themes}" EmptyView="pas de data"> <CollectionView.ItemTemplate > <DataTemplate x:DataType="model:Theme"> <VerticalStackLayout Spacing="300000" > <ImageButton x:Name="ThemeButton" Source="{Binding LogoTheme }" HeightRequest="50" Clicked="ThemeButton_Clicked" ToolTipProperties.Text="{Binding Intitule}" CornerRadius="100" ></ImageButton> </VerticalStackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> <CollectionView x:Name="sousthemes" ItemsSource="{Binding Audit.SousThemes}" IsVisible="false"> <CollectionView.ItemTemplate> <DataTemplate x:DataType="model:SousTheme"> <VerticalStackLayout> <Button Text="{Binding Intitule}" WidthRequest="350" Clicked="SousTheme_Clicked" BackgroundColor="#AE1439" ></Button> </VerticalStackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> <CollectionView x:Name="question" ItemsSource="{Binding Audit.Questions}" IsVisible="false" HorizontalScrollBarVisibility="Always" VerticalScrollBarVisibility="Always"> <CollectionView.ItemTemplate> <DataTemplate x:DataType="model:Question"> <VerticalStackLayout WidthRequest="900"> <mct:Expander x:Name="questionz" HeightRequest="220" IsExpanded="False"> <mct:Expander.Header> <Grid BackgroundColor="LightGray" HeightRequest="100"> <Label Text="{Binding Intitule}" FontSize="16" Grid.Column="0"></Label> <Image Source="exclamation_mark.png" HeightRequest="20" TranslationX="380" ToolTipProperties.Text="{Binding RappelJuridique}"/> </Grid> </mct:Expander.Header> <mct:Expander.Content> <StackLayout > <HorizontalStackLayout > <RadioButton x:Name="conforme" Content="Conforme" IsChecked="{Binding IsConforme, Mode=TwoWay}" Value="true" CheckedChanged="RadioButton_CheckedChanged" > </RadioButton> <RadioButton x:Name="nonConforme" Content="Non conforme" IsChecked="{Binding IsConforme, Mode=TwoWay}" Value="false" CheckedChanged="RadioButton_CheckedChanged"> </RadioButton> <RadioButton x:Name="nonFait" Content="non Fait" IsChecked="{Binding IsConforme, Mode=TwoWay}" Value="null" CheckedChanged="RadioButton_CheckedChanged"> </RadioButton> </HorizontalStackLayout> <Entry Placeholder="Commentaire" Text="{Binding Commentaire}" HeightRequest="100" ></Entry> </StackLayout> </mct:Expander.Content> </mct:Expander> </VerticalStackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView> <VerticalStackLayout> <Button x:Name="record_audit" Text="enregistrer Audit" BackgroundColor="#AE1439" Command="{Binding RecordAudit}"> </Button> <Button x:Name="generate_PDF" Text="Générer PDF" BackgroundColor="#AE1439" Command="{Binding GeneratePdf}"> </Button> </VerticalStackLayout> </HorizontalStackLayout> </ContentPage> 隐藏代码 using CommunityToolkit.Maui.Views; using FntrAudit.DALApi; using FntrAudit.Models; using FntrAudit.Services; using FntrAudit.ViewModel; namespace FntrAudit.Views; public partial class NewAudit : ContentPage { private ISocieteUserService _societeUserService; private IClientService _clientService; private UserContext _userContext; public RestService<Theme> _themeService = new RestService<Theme>(); public RestService<Audit> _themeServices = new RestService<Audit>(); public List<Theme>Themes { get; set; } public List<SousThemeGroup> SousThemes { get; set; } = new List<SousThemeGroup>(); public NewAudit(UserContext ContextCurrent) { InitializeComponent(); _userContext = ContextCurrent; try { BindingContext = new NewAuditViewModel(ContextCurrent); } catch (Exception ex) { var tu = ex.Message; } } private void ThemeButton_Clicked(object sender, EventArgs e) { question.IsVisible = false; var imgbtn = (ImageButton)sender; Theme btnThemeClicked = (Theme)imgbtn.BindingContext; if (btnThemeClicked.SousThemes != null) { sousthemes.IsVisible = true; sousthemes.ItemsSource = btnThemeClicked.SousThemes; } else { sousthemes.IsVisible = false; question.IsVisible = true; question.ItemsSource = btnThemeClicked.Questions; } } private void SousTheme_Clicked(object sender, EventArgs e) { var btn = (Button)sender; SousTheme btnSousThemeClicked = (SousTheme)btn.BindingContext; // btn.BackgroundColor = Colors.LightGray; question.IsVisible = true; question.ItemsSource = btnSousThemeClicked.Questions; } private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e) { //if (sender is RadioButton radioButton && radioButton.BindingContext is Question question) //{ // if (radioButton.IsChecked) // { // // Accédez à la valeur du RadioButton // string value = radioButton.Value.ToString(); // // Mettez à jour la propriété IsConforme de l'objet Question // question.IsConforme = bool.Parse(value); // } //} if (sender is RadioButton radioButton && radioButton.BindingContext is Question question) { if (radioButton.Value.ToString() == "true") { question.IsConforme = true; // if (e.Value) // { // question.IsConforme = true; // } } else if (radioButton.Value.ToString() == "false") { question.IsConforme = false; //if (e.Value) //{ // question.IsConforme = false; // } } else if (radioButton.Value.ToString() == "null") { question.IsConforme = null; //if (e.Value) //{ // question.IsConforme = null; // } } } } private void record_audit_Clicked(object sender, EventArgs e) { var audit = e; } } 视图模型: using FntrAudit.DALApi; using FntrAudit.Models; using FntrAudit.Services; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using CommunityToolkit.Mvvm.ComponentModel; using FntrAudit.DocumentGenerators; using System.Text.Json; using Microsoft.VisualBasic; using FntrAudit.Utils; using FntrAudit.Data; using Microsoft.EntityFrameworkCore; namespace FntrAudit.ViewModel { public partial class NewAuditViewModel : INotifyPropertyChanged { private UserContext _currentContext; private bool isSousThemeVisible; private RestService<Theme> _themeService = new RestService<Theme>(); private RestService<Audit> _auditService = new RestService<Audit>(); private RestService<Client> _clientService = new RestService<Client>(); private Audit audit; SqliteDbContext db = new SqliteDbContext(); private bool? isConforme; public bool? IsConforme { get { return isConforme; } set { if (isConforme != value) { isConforme = value; OnPropertyChanged(nameof(IsConforme)); } } } public bool IsSousThemesVisible { get => isSousThemeVisible; set { SetProperty(ref isSousThemeVisible, value); } } public Audit Audit { get => audit; set { audit = value; OnPropertyChanged(nameof(Audit)); } } public List<Audit> AuditList { get; } =new List<Audit>(); public List<Theme> Themes { get; } = new List<Theme>(); public ICommand RadioButtonCommand { get; } bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null) { if (Object.Equals(storage, value)) return false; storage = value; OnPropertyChanged(propertyName); return true; } public NewAuditViewModel(UserContext ContextCurrent) { _currentContext = ContextCurrent; if (_currentContext.IsReprise) { Audit = JsonSerializer.Deserialize<Audit>(_currentContext.CurrentClient.AuditEncours); } else { if (Constantes.IsBeta) { Audit = db.Audit .Where(a => a.Id == 1) .Include(a => a.Themes) .Include(a => a.SousThemes) .Include(a => a.Questions) .FirstOrDefault(); } else { Audit = _auditService.Get(@"https://localhost:7114/api/Audit/" + 1, new Audit()); } } RecordAudit = new Command(AuditRecord); GeneratePdf = new Command(GeneratePDF); GetSoustheme = new Command(GetSousthemes); } private void HandleRadioButton(string value) { // Logique de gestion de RadioButton ici Console.WriteLine($"RadioButton sélectionné : {value}"); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public ICommand RecordAudit { get; set; } public ICommand GeneratePdf { get; set; } public ICommand GetSoustheme { get; set; } protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public void AuditRecord() { Client client = new Client(); if (Constantes.IsBeta) { client = db.Client.Where(c => c.Id == _currentContext.ClientId).FirstOrDefault(); } else { client = _clientService.Get(@"https://localhost:7114/api/Client/" + _currentContext.ClientId, new Client()); } var a = Audit; client.AuditEncours = JsonSerializer.Serialize(Audit); if (Constantes.IsBeta) { db.Client.Update(client); db.SaveChanges(); } else { _clientService.Create(client, @"https://localhost:7114/api/Client/updateClient", false); } _currentContext.CurrentClient.AuditEncours = client.AuditEncours; } public void GeneratePDF() { DocumentGenerator documentGenerator = new DocumentGenerator(); DocumentContent documentContent = new DocumentContent() { Audit = Audit, Client = _currentContext.CurrentClient, User = _currentContext.CurrentUser }; documentGenerator.GenerateDoc(documentContent); } public void GetSousthemes() { IsSousThemesVisible = true; } } } 审核模型: using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FntrAudit.Models { public class Audit : BaseEntity, INotifyPropertyChanged { private string code; private List<Theme> themes; private List<SousTheme> sousThemes; private List<Question> questions; private List<QuestionLiee> questionsLiees; public string Code { get { return code; } set { code = value; OnPropertyChanged(nameof(Code)); } } public List<QuestionLiee> QuestionsLiees { get { return questionsLiees; } set { questionsLiees = value; OnPropertyChanged(nameof(QuestionsLiees)); } } public List<Question> Questions { get { return questions; } set { questions = value; OnPropertyChanged(nameof(Questions)); } } public List<SousTheme> SousThemes { get { return sousThemes; } set { sousThemes = value; OnPropertyChanged(nameof(SousThemes)); } } public List<Theme> Themes { get { return themes; } set { themes = value; OnPropertyChanged(nameof(Themes)); } } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } } 问题模型: using System.ComponentModel; namespace FntrAudit.Models { public class Question: BaseEntity { public int ThemeId { get; set; } public int AuditId { get; set; } public int? SousThemeId { get; set; } public string RappelJuridique { get; set; } public string Code { get; set; } public string Preconisation { get; set; } public bool? IsConforme { get; set; } public bool HasLinkedQuote { get; set; } public string? Commentaire { get; set; } //private int themeId; //private int auditId; //private int? sousThemeId; //private string rappelJuridique; //private string code; //private string preconisation; //private bool? isConforme; //private bool hasLinkedQuote; //private string commentaire; //public int ThemeId //{ // get { return themeId; } // set { themeId = value; OnPropertyChanged(nameof(ThemeId)); } //} //public int AuditId //{ // get { return auditId; } // set { auditId = value; OnPropertyChanged(nameof(AuditId)); } //} //public int? SousThemeId //{ // get { return sousThemeId; } // set { sousThemeId = value; OnPropertyChanged(nameof(SousThemeId)); } //} //public string RappelJuridique //{ // get { return rappelJuridique; } // set { rappelJuridique = value; OnPropertyChanged(nameof(RappelJuridique)); } //} //public string Code //{ // get { return code; } // set { code = value; OnPropertyChanged(nameof(Code)); } //} //public string Preconisation //{ // get { return preconisation; } // set { preconisation = value; OnPropertyChanged(nameof(Preconisation)); } //} //public bool? IsConforme //{ // get { return isConforme; } // set { isConforme = value; OnPropertyChanged(nameof(IsConforme)); } //} //public bool HasLinkedQuote //{ // get { return hasLinkedQuote; } // set { hasLinkedQuote = value; OnPropertyChanged(nameof(HasLinkedQuote)); } //} //public string? Commentaire //{ // get { return commentaire; } // set { commentaire = value; OnPropertyChanged(nameof(Commentaire)); } //} //public event PropertyChangedEventHandler PropertyChanged; //void OnPropertyChanged(string name) => // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } } 我尝试了很多东西,但当我加载包含属性 isconforme 未绑定的对象审计时,没有任何效果。当我保存审计时,我有 isconforme 的良好价值,并且在我的数据库中没有问题。当我在代码中查看该属性时,该属性很好。 提前致谢 根据@Jason 评论,您不能将所有三个单选按钮绑定到同一个布尔值。另外,因为您的问题视图模型有 TwoWay 绑定,所以您不需要实现 CheckedChanged。您将可以访问问题视图模型中的更改。 在下面,我更改了您的问题视图模型,使其具有 3 个布尔值,并且每个布尔值都会升高 PropertyChanged?。然后,在 Audit 视图模型中,我连接到 PropertyChanged? 事件并做出相应的反应。我提供了一个模拟 MainPage.xaml 来演示 Binding,并且所有内容都已连接,无需实现 CheckedChanged,除了视图模型中的内容之外,我的代码隐藏中也不需要任何特殊代码。 // Question.cs ... public class Question : INotifyPropertyChanged { private bool _isConforme; public bool IsConforme { get { return _isConforme; } set { _isConforme = value; OnPropertyChanged(nameof(IsConforme)); } } private bool _nonConforme; public bool NonConforme { get { return _nonConforme; } set { _nonConforme = value; OnPropertyChanged(nameof(NonConforme)); } } private bool _nonFait; public bool NonFait { get { return _nonFait; } set { _nonFait = value; OnPropertyChanged(nameof(NonFait)); } } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } // Audit.cs ... public class Audit : INotifyPropertyChanged { public IList<Question> Questions { get; } = new List<Question>(); public Audit() { Questions.Add(new Question()); Questions.Add(new Question()); for (int i = 0; i < Questions.Count; i++) { var currentQuestion = Questions[i]; currentQuestion.PropertyChanged += (s, e) => Question_PropertyChanged(i, currentQuestion); } } private void Question_PropertyChanged(int index, Question currentQuestion) { Debug.WriteLine($"Question_PropertyChanged: {index} {currentQuestion.IsConforme} {currentQuestion.NonConforme} {currentQuestion.NonFait}"); } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } // MainPage.xaml.cs public partial class MainPage : ContentPage { public Audit Audit { get; } = new Audit(); public MainPage() { BindingContext = this; InitializeComponent(); } } <!-- MainPage.xaml ... --> <CollectionView ItemsSource="{Binding Audit.Questions}"> <CollectionView.ItemTemplate> <DataTemplate x:DataType="{x:Type local:Question}"> <HorizontalStackLayout> <RadioButton Content="Conforme" IsChecked="{Binding IsConforme, Mode=TwoWay}"/> <RadioButton Content="Non conforme" IsChecked="{Binding NonConforme, Mode=TwoWay}"/> <RadioButton Content="non Fait" IsChecked="{Binding NonFait, Mode=TwoWay}"/> </HorizontalStackLayout> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView>

回答 1 投票 0

表单仅在刷新页面时显示

我正在尝试创建一个模拟冰淇淋店网站以改进我的 JavaScript。该页面的问题在于,每个单选按钮在被选中时都会显示两种表单之一。

回答 1 投票 0

如何更改悬停时选中的单选按钮的颜色?

<!-- This is my html input tag --> <input class="custom-radio" type="radio" name="selected_center" value="{{center.id}}" id="selected_center_{{center.id}}"> <!-- Only CSS I am using for my custom color using accent property but cant change the color on hover --> input[type='radio'] { accent-color: #0098ff; } 我已将悬停属性与输入单选标签一起使用,但不起作用。 不确定您为什么要这样做,但您可以做到。 body { margin: 1em; font-family: sans-serif; } .custom-radio>label { display: block; font-size: 1.5em; margin: 0.25em 0; } .custom-radio>label>input[type=radio] { transform: scale(2); vertical-align: middle; margin-right: 1em; } .custom-radio>label:nth-child(1)>input[type=radio] { accent-color: red; } .custom-radio>label:nth-child(2)>input[type=radio] { accent-color: darkorange; } .custom-radio>label:nth-child(3)>input[type=radio] { accent-color: green; } .custom-radio>label:hover { color: blue; } .custom-radio>label:hover>input[type=radio] { accent-color: blue; } .custom-radio>label>input[type=radio]:not(:checked) { cursor: pointer; } <div class="custom-radio"> <label><input type="radio" name="radio1"> Stop</label> <label><input type="radio" name="radio1"> Wait</label> <label><input type="radio" name="radio1"> Go</label> </div>

回答 1 投票 0

如何在编辑表单中设置单选按钮选定的值

我正在使用 ReactiveFormModule 创建编辑表单。我想在编辑表单中显示数据,例如文本框、下拉列表、单选框和复选框。我可以使用 setValue 成功设置文本框和下拉列表的值

回答 1 投票 0

如何在两个单选按钮之间切换,并且在reactjs中单击它时应该打开一个单独的组件

我正在创建一个登录页面,它有两个不同的组件,分别名为管理员登录和客户登录,它们在登录表单中显示为单选按钮,如果我单击客户登录单选按钮,它应该...

回答 1 投票 0

AngularJS 取消选中单选按钮

我尝试使用 ng-dblclick、$event 等寻找其他解决方案,但它们似乎都不适合我。 我在表格中的 ng-repeat 内有两个单选按钮。我有一个字段用于 s...

回答 2 投票 0

React - 基于单选按钮选择动态更新 UI

问题 我正在开发一个网络应用程序功能,可以计算特定区域的隔热系数。您可以在此处查看实时 Codesandbox 示例(这是带有

回答 1 投票 0

Android 自定义视图,其行为类似于 RadioButton/CheckBox

我有以下自定义视图 有 2 个文本字段和一个 RadioButton,我希望它的行为像 RadioButton(当涉及到对讲时) 现在: 当选择 customRadioButton 时,它说

回答 1 投票 0

在 Drupal Webforms YAML 中有条件地预选单选按钮

我正在尝试有条件地预选 Drupal Webforms 中的单选按钮(我认为这必须使用 YAML 来完成?)。 我有一个隐藏字段“ref”,它可以有 4 个值中的 1 个(de、fr、it、en)。

回答 1 投票 0

Python - 我的 CTkRadioButtons 接受多个选择

我对三个单选按钮有一个问题,它们接受一次性选择所有按钮。我该如何防止这种事情,当我按下另一个按钮时,选择会返回...

回答 1 投票 0

单选按钮选中香草js时显示div

这里有点挣扎。我的事件侦听器仅检测到单选按钮的“已选中”更改,而不是“未选中”更改。注意:仅纯 javascript (vanillajs),无 jQuery。 一点 JSFiddle

回答 2 投票 0

根据先前的组禁用单选按钮组

我有 2 组 2 个单选按钮。除非在第 1 组中选择“是”,否则应禁用第 2 组。但是,如果用户从“是”切换,则需要清除并禁用第二组

回答 1 投票 0

动态加载数据时,如何为 Angular 中的数组分配默认值?

我有一个名为“习惯”的数组。像这样 我将其显示在带有单选按钮的表格中(是,否)。 我有一个名为“习惯”的数组。像这样 我将其显示在带有单选按钮的表格中(是,否)。 <tr *ngFor="let habits of masterDate.data.habits; index as x"> <th class="tbl-border-right-bottom text-wrap" style="width: 50%"> {{ habits.name }} <div hidden="true"> <input type="text" formControlName="habitsSufferedMainId{{ x }}" /> </div> </th> <td class="tbl-border-right-bottom"> <div class="col-md-6"> <div class="form-group d-flex justify-content-around pr-2"> <div class="custom-control custom-radio"> <input type="radio" class="custom-control-input" id="habitsSufferedMain{{ x }}" value="1" name="habitsSufferedMain{{ x }}" formControlName="habitsSufferedMain{{ x }}" /> <label class="custom-control-label" for="habitsSufferedMain{{ x }}">Yes</label> </div> <div class="custom-control custom-radio"> <input type="radio" class="custom-control-input" id="isMainHabitNo{{ x }}" value="0" name="habitsSufferedMain{{ x }}" formControlName="habitsSufferedMain{{ x }}" [checked]="true" /> <label class="custom-control-label" for="isMainHabitNo{{ x }}">No</label> </div> </div> </div> <div class="form-group"> <textarea placeholder="If 'Yes' please give details" class="form-control form-control-sm" rows="2" formControlName="habitsSufferedMainReason{{ x }}"></textarea> </div> </td> </tr> 如何将默认值设置为“否”。 根据点击是或否,将值放入数组中。 首先创建一个地图来存储结果 const results = new Map<number, boolean>(); 您可以使用 FormControl() 设置复选框的默认值 const habitOne = new FormControl(false); results['habitOne'].set(false); 然后监听 valueChanges oberbable 将结果存储在 Map 中 habitOne.valueChanges.subscribe(value => { results.set('habitOne', value) }) 当然,您可以使用 FormBuilder 通过循环来完成此操作。 (但请记住以反应形式包装所有输入<form [formGroup]=form> form: FormGroup; constructor(private fb: FormBuilder) { this.form = this.fb.group( this.habits.reduce((controls, habit, index) => { this.results.set(index, false) return { ...controls, [`habit${index}`]: new FormControl(false) } }, {})); Object.keys(this.form.controls).forEach((control, index) => { control.valueChanges.subscribe(value => { this.results.set(index, value == true) }) } }

回答 1 投票 0

防止单选按钮下方的文本换行

我能描述我想要的最好的方式就是用这张图片: 如何使文本与顶部文本对齐,而不是与单选按钮对齐? 相关CSS如下: .basic-灰色{ 宽...

回答 5 投票 0

如何取消选中单选按钮

我有两种表单,一种带有单选按钮,用户必须选择该按钮才能编辑。 [表格名称=“A”] [输入类型=“无线电”名称=“BookItem”值=“1”/] [输入类型=“无线电”名称=“” 我有两种表单,一种带有单选按钮,用户必须选择该按钮才能编辑。 [form name="A"] <li>[input type="radio" name="BookItem" value="1" /]</li> <li>[input type="radio" name="BookItem" value="2" /]</li> <li>[input type="radio" name="BookItem" value="3" /]</li> [form]<p> 从表单 (A) 中选择“BookItem”后,我调用 $("#EditFormWrapper").load("callEditData.cfm? ID="+ID); 函数来加载第二个表单 (B) <div id="EditFormWrapper"><div></p> <!---// begin dynamic form generated by external file callEditData.cfm //---> [form id="editForm" name="B"] <ul class="hourswrapper"> <li><input type="checkbox" id="TOR2Hours" class="TOR2Hours" name="TOR2Hours" value="AM2Hrs1" /> 2 Hours AM</li> <li><input type="checkbox" id="TOR2Hours" class="TOR2Hours" name="TOR2Hours" value="PM2Hrs1" /> 2 Hours PM</li> <li><input type="checkbox" id="TOR2Hours" class="TOR2Hours" name="TOR2Hours" value="AM2Hrs2" /> 2 Hours AM</li> <li><input type="checkbox" id="TOR2Hours" class="TOR2Hours" name="TOR2Hours" value="PM2Hrs2" /> 2 Hours PM</li> </ul> [input type="image" src="images/submit-btn.gif" id="addBTN" name="addBTN" class="buttons" alt="SubmitRrequest" /] [input type="image" src="images/cancel-btn.gif" id="editBTNcancel" name="editBTNcancel" class="buttons" alt="Cancel Request" /] [/form] <!---// end dynamic form from external file //---> 当用户单击表单(B)中的取消按钮(editBTNcancel)时,我想取消选中表单(A)上的单选按钮。 这是我的脚本: $("#editBTNcancel").live("click", function(event){ event.preventDefault(); $("#EditFormWrapper").slideUp("fast").empty(); //$('.TOR2Hours').removeAttr('checked'); $('.TOR2Hours').attr('checked', false); }); 我希望我清楚地说明我的问题,任何建议将不胜感激! 您可以像这样访问表格... var exampleForm = document.forms['form_name']; 然后循环遍历表格 for( var i=0; i<exampleForm.length; i++ ){ alert( exampleForm[i].type ); } 你可以像这样测试检查... if( exampleForm[i].checked ) 要取消选择选中的单选按钮,请尝试... exampleForm[i].checked=false; 最终的代码看起来像这样...... var exampleForm = document.forms['form_name']; for( var i=0; i<exampleForm.length; i++ ){ if( exampleForm[i].type ) == 'radio' && exampleForm[i].checked == true ){ exampleForm[i].checked = false; } } 我不确定你到底想要什么,但你可以尝试使用重置输入。 <input type='reset' /> 由于这几乎是最简单的 DOM 任务并且适用于每个可编写脚本的浏览器,因此我建议不要使用 jQuery 方法: $(".TOR2Hours")[0].checked = false; 我想到的另一件事是你的选择器是否正确。您的意思是按类选择一组元素还是应该使用 ID 选择器? 您的选择器根本就是错误的。 如果您想取消选中第一个表单中的单选按钮,您应该使用 $('input[name="BookItem"]') 而不是 $('.TOR2Hours') : $("#editBTNcancel").on("click", function(event){ $("#EditFormWrapper").slideUp("fast").empty(); $('input[name="BookItem"]').attr('checked', false); }); 至于使用哪种方法取消选中单选按钮,以下 3 种方法应该都有效: $('input[name="BookItem"]').attr('checked', false); $('input[name="BookItem"]').removeAttr('checked'); $('input[name="BookItem"]').prop('checked', false); 但是,请查看关于 jQuery prop() 的 jQuery 文档,了解 attr() 和 prop() 之间的区别。 我刚刚发现了这个问题的一个很好的解决方案。 假设您有两个无线电需要能够选中/取消选中,请实现此代码并更改必要的内容: var gift_click = 0; function HandleGiftClick() { if (document.getElementById('LeftPanelContent_giftDeed2').checked == true) { gift_click++; memorial_click = 0; } if (gift_click % 2 == 0) {document.getElementById('LeftPanelContent_giftDeed2').checked = false; } } var memorial_click = 0; function HandleMemorialClick() { if (document.getElementById('LeftPanelContent_memorialDeed2').checked == true) { memorial_click++; gift_click = 0; } if (memorial_click % 2 == 0) { document.getElementById('LeftPanelContent_memorialDeed2').checked = false; } } :)不客气 我用这种方式解决了你在ASP.net中的问题,检查这个.. radioButton.InputAttributes["show"] = "false"; string clickJs = " if(this.show =='true'){this.show ='false'; this.checked = false;}else{this.show='true'; this.checked = true;};"; radioButton.Attributes["onClick"] = clickJs; 在asp.net中,可以使用这种方式添加属性。您还可以手动向radioButton添加属性,并执行函数(clickJs)来更改ckecked属性!!! Array.prototype.forEach.call(auswahl_dachaufbau, function(rd) { // Event-Listener für Radio-Buttons rd.addEventListener("mousedown", function(e) { let id = e.target.id; if (this.checked) { reset_dachaufbau(id); // Das Setzen der onclick-function auf diese Weise ist dafür da, damit man // Radio-Buttons auch unchecked machen kann ohne einen neuen Radio-Button auszuwählen // Seltsamerweise ist das nicht möglich ohne weiteres this.onclick = function() { this.checked = false; } } else { this.onclick = null; select_dachaufbau(id); } }); }); Funktioniert 沉浸式

回答 7 投票 0

我的两个单选按钮在相同的 HTML 代码下表现不同

在下面的代码中,当我单击男性文本(不是单选按钮)时,它会选择该选项,但是当我单击女性文本时,它会选择单选按钮。如何?请帮忙。 ` 在下面的代码中,当我单击男性文本(不是单选按钮)时,它会选择该选项,但是当我单击女性文本时,它会选择单选按钮。如何?请帮忙。 `<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <form action="action.php"> <lable for="id1"> <input type="radio" value="Male" name="Gender" id="id1">Male </lable> <label for="id2"> <input type="radio" value="Female" name="Gender" id="id2">Female </label> </form> </body> </html>` 我期望通过单击男性文本来选择男性单选按钮,但只有在单击男性单选按钮后才会选择它。 问题是两个单选按钮具有相同的名称属性。 <lable for="Male"> <input type="radio" value="Male" name="Male" id="id1">Male </lable> <label for="Female"> <input type="radio" value="Female" name="Female" id="id2">Female </label>

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.