【问题标题】:Verify all radio groups have a value checked using a generic selector使用通用选择器验证所有无线电组都有一个值检查
【发布时间】:2022-12-01 01:20:37
【问题描述】:

如何验证所有无线电组使用通用选择器和 .each() 函数至少选择了 1 个值。

我找到的所有示例都需要使用单个无线电选项的 ID 或名称,而不是组。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    尝试这个:

    const radios = {};
    
    $('input[type="radio"]').each((i, e) => {
      let $radio = $(e);
      
      if ($radio.is(':checked')) {
        radios[$radio.attr('name')] = $radio.val();
      }
    });
    
    console.log(radios);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <input type="radio" value="1" name="ri1">
    <input type="radio" value="2" name="ri1" checked="checked">
    <input type="radio" value="3" name="ri1">
    
    <input type="radio" value="1" name="ri2">
    <input type="radio" value="2" name="ri2">
    <input type="radio" value="3" name="ri2" checked="checked">

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-04
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      相关资源
      最近更新 更多