【问题标题】:check radio group value at same index检查同一索引处的无线电组值
【发布时间】:2021-07-04 23:30:04
【问题描述】:

我有两个无线电组,当我获得第一组检查值时,我无法使用以下代码在同一索引处设置另一个组:

$('input[name="service_type[]"]:checked').each(function(index) {        
        $("input[name=service_type1["+index+"]]").attr('checked', 'checked');
        });

语法错误,无法识别的表达式:input[name=service_type1[0]]

two input radio button groups named Single and Return

     $service.='<input type="radio" id="str"'.$i++.'"
        name="service_type1[]"
        class="radio-input return" 
        value="'.$request->service_type.'">';


$service.='<input type="radio" id="st"'.$i.'"
        name="service_type[]"
        class="radio-input single" 
        value="'.$request->service_type.'">';

【问题讨论】:

  • 同时显示 html 代码。
  • @Swati 问题已更新
  • 所以您需要检查所有其他 divs 轿车、房地产 ..etc 中的相同收音机?
  • 如果单选同一辆车,我想检查返回按钮
  • 你可以试试this

标签: jquery


【解决方案1】:

使用 attr 的目标元素应该有 ",你错过了它们。

代替

$("input[name=service_type1["+index+"]]")

$('input[name="service_type1[' + index + ']"]');

【讨论】:

  • 您真的有输入名称为service_type1[0]service_type1[1] 等吗?
  • 是的,我有它的名字service_type1[]service_type[]
  • 你看出区别了吗?您的代码将针对 service_type1[0]service_type1[1] 等,具体取决于您拥有多少 input[name="service_type[]"]:checked 元素。您发布说您拥有service_type1[],但没有service_type1[0]。我建议添加 html 代码并添加更多关于您所追求的结果的解释。甚至可以添加可以直观地解释最终结果的图像
  • 我已经用图像更新了它,其中有两个输入,并且基于选择的第一个,我必须在同一索引处选择另一个
  • 您可以添加另一张图片来显示您想要的结果吗?甚至可能是一个屏幕演员?
【解决方案2】:
$('input[name="service_type1[]"]:eq('+index+')').prop('checked', true);

解决了:eq是jquery中使用的索引选择器的问题

【讨论】:

    猜你喜欢
    • 2014-04-27
    • 2012-04-21
    • 2019-05-11
    • 2012-11-30
    • 1970-01-01
    • 2020-08-20
    • 2015-12-24
    • 1970-01-01
    • 2012-03-24
    相关资源
    最近更新 更多