简体   繁体   English

限制用户只能从具有相同值的两组中选择一个单选按钮

[英]Restrict user to select only one radio button from two groups with same value

I am working on a web app where I am creating a virtual team.我正在开发一个网络应用程序,我正在创建一个虚拟团队。 User can pick players and then select a "Captain" and a "Vice-Captain" of the team.用户可以选择球员,然后选择球队的“队长”和“副队长”。 To accomplish this I have created two radio button groups so that user can pick only one player as "Captain" and only one player as "Vice-Captain" of the team.为了实现这一点,我创建了两个单选按钮组,以便用户只能选择一名球员作为“队长”,并且只能选择一名球员作为球队的“副队长”。 My problem is that how can I restrict user from picking same player as Captain and Vice-Captain as both radio groups have different names and user can select one from each even if it is the same player.我的问题是,我如何限制用户选择与队长和副队长相同的玩家,因为两个无线电组都有不同的名称,即使是同一个玩家,用户也可以从中选择一个。 Here is a sample code in Laravel Blade of what I am trying to explain.这是我试图解释的 Laravel Blade 中的示例代码。 You can suggest a solution using javascript or jquery.您可以使用 javascript 或 jquery 提出解决方案。

    @foreach($players as $player)
    <tr>
      <td>
        <input type="checkbox" name="player[]" value="{{ $player->id }}">
      </td>
      <td>
        {{ $player->name }}
      </td>
      <td>
        {{ $player->category }}
      </td>
      <td>
        {{ $player->team }}
      </td>
    </tr>
    <tr class="table-borderless">
      <td colspan="2">
        <input type="radio" name="captain" value="{{ $player->id }}"> Captain
      </td>
      <td colspan="2">
        <input type="radio" name="vicecaptain" value="{{ $player->id }}"> Vice-Captain
      </td>
    </tr>
@endforeach

I want to restrict user from selecting both 'Captain' and 'Vice-Captain' radio buttons for the same player and want to show a helpful message to change one of the selection to a different player.我想限制用户为同一玩家同时选择“队长”和“副队长”单选按钮,并希望显示有用的消息来将选择之一更改为不同的玩家。

I solved my problem by following suggestions in the following question's answer我按照以下问题的答案中的建议解决了我的问题

[ Compare radio button values using jquery [ 使用 jquery 比较单选按钮值

Thanks for looking.谢谢你看。

Use Javascript to see checked value for captain.使用 Javascript 查看船长的检查值。 Use event listener to listen for value change on the captain group.使用事件监听器监听船长组的值变化。 Based on the checked value, disable that id from vicecaptain list.根据选中的值,从副队长列表中禁用该 id。

Checking or querying dom elements will be easier if you gave the fields a data attribute or a unique class name or id.如果您为字段提供数据属性或唯一的类名或 id,则检查或查询 dom 元素会更容易。

Have the same backend validation too.也有相同的后端验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM