Jquery 切换状态前如何检查其他收音机输入

Jquery 切换状态前如何检查其他收音机输入,jquery,Jquery,以下代码可以工作,但在找到div.alert-warning之前有多个行同级,这会在更改分数时导致问题。当一行为“2”时,它显示警告,如果我更改该行,它将消失,但当它消失时,如果有另一行的分数为2,我需要它继续显示警告。当一行更改为>2时,在隐藏元素之前,如何更改以下内容以检查是否有任何其他收音机的分数为2或更低?任何帮助都会很棒 $('input:radio').change(function(){ if($(this).is(':checked') && $(thi

以下代码可以工作,但在找到div.alert-warning之前有多个行同级,这会在更改分数时导致问题。当一行为“2”时,它显示警告,如果我更改该行,它将消失,但当它消失时,如果有另一行的分数为2,我需要它继续显示警告。当一行更改为>2时,在隐藏元素之前,如何更改以下内容以检查是否有任何其他收音机的分数为2或更低?任何帮助都会很棒

$('input:radio').change(function(){
     if($(this).is(':checked') && $(this).val() <= 2) {
          $(this).closest('tr').siblings().find('div.alert-warning').show();
     } else {
          if($(this).is(':checked') && $(this).val() > 2) {
                 $(this).closest('tr').siblings().find('div.alert-warning').hide();
          }
     }
});
$('input:radio')。更改(函数(){
if($(this).is(':checked')&&&$(this.val()2){
$(this).closest('tr').sides().find('div.alert-warning').hide();
}
}
});
HTML


@foreach($evaluationForm作为$form)
{{$form->title}
分数
@如果($表格->问题1)
{{$form->problem_1}
4.
3.
2.
1.
0
NA
@恩迪夫
@如果($表格->问题2)
{{$form->problem_2}
4.
3.
2.
1.
0
NA
@恩迪夫
需要评论评分为2分或更低时。
评论
@endforeach
实际上,最多可能出现5个问题,我将代码缩短为2个问题,因为您会明白这一点。

$('input:radio')。在('change',function()上{
//仅使用更改的单选按钮所属表格的单选按钮
var$tbodyRadios=$(this).closest('tbody').find('input:radio');
//查找警告元素
var$warning=$(this).closest('tr').sides().find('div.alert-warning');
//查看检查单选按钮的值是否为2或更小

如果($tbodyRadios.filter(function(index,radio){return radio.checked&&radio.value),您是说当它落入else逻辑时,它会隐藏其他行中的收音机,这些行中的收音机的值小于两个?侧注;请勿
$(this)。是(':checked'))
。如果您已经有对元素的引用,请使用
this。直接选中
属性。@Taplar correct。这些行都是一个公共类别的一部分。每一行都是与该类别相关的不同问题。警告出现在注释框上方的行末尾,警告他们如果分数为2,则需要注释或更少。您要隐藏的注释是否与您选择的收音机位于同一行?还是所有行只有一条注释?@Taplar否,这是一条与类别相关的引导警报。现在,当选择它们时,不会发生任何事情。控制台中没有警告。@twoarmtom我在筛选器中有一个错误。第一个argu要筛选的元素是索引,而不是元素。请再试一次。这很好,但还有一个问题。我正在使用新元素循环每个类别。代码也影响其他表。我猜您可以限制为当前表?我不确定这将如何影响多个表,因为它正在siblin中查找具有的divg
tr
的单选按钮已更改。其他表中的
tr
不会是同级。此时,我真的需要查看您的html示例。或者等等,您是说其他表中的单选按钮正在影响更改的单选按钮所属的表?@twoarmtom好的,已修改。它应该只计算按下表中的单选按钮,以决定是否显示该表的警告。
<table class="table table-striped" id="table">
          @foreach ($evaluationForm as $form)
          <tbody class="mb-3">
            <tr class="bg-primary">
              <td class="header">{{ $form->title }}</td>
              <td class="header">SCORE</td>
            </tr>
            @if ($form->question_1)
            <tr>
              <td>{{ $form->question_1 }}</td>
              <input type="hidden" name="{{ $form->category }}_question_1" value="{{ $form->question_1 }}">
              <td class="text-nowrap text-right">
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_1" value="4">
                  <label>4</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_1" value="3">
                  <label>3</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_1" value="2">
                  <label>2</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_1" value="1">
                  <label>1</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_1" value="0">
                  <label>0</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_1" value="3.1">
                  <label>NA</label>
                </div>
              </td>
            </tr>
            @endif
            @if ($form->question_2)
            <tr>
              <td>{{ $form->question_2 }}</td>
              <input type="hidden" name="{{ $form->category }}_question_2" value="{{ $form->question_2 }}">
              <td class="text-nowrap text-right">
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_2" value="4">
                  <label>4</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_2" value="3">
                  <label>3</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_2" value="2">
                  <label>2</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_2" value="1">
                  <label>1</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_2" value="0">
                  <label>0</label>
                </div>
                <div class="form-check form-check-inline">
                  <input required type="radio" name="{{ $form->category }}_2" value="3.1">
                  <label>NA</label>
                </div>
              </td>
            </tr>
            @endif
            <tr>
              <td colspan=2>
                  <div class="alert alert-warning show" role="alert">
                      <strong>A comment is required!</strong> When a score of 2 or less is given.
                    </div>
                <strong>Comments</strong><br>
                <textarea id="comment" class="form-control comment" name="{{ $form->category }}_comment" rows="5"></textarea>
              </td>
            </tr>
          </tbody>
          @endforeach
        </table>