如何检查是否在表单中单击了动态生成的单选按钮?

我正在使用 php 生成一个带有多项选择题的表单,现在我想通过检查是否单击了每个问题的单选按钮来检查每个问题是否已被回答。


<div class="opt">

    <div class="row1">                                                 

      <label class="label">{{ $question->question }}</label>

    </div>

<div class="ans">                                                            

  $answer=$answers[$question->id]

  @foreach ($answer as $answer)

     <label class="btn btn-default no-margin-rule" >

        <input type="radio" name="{{$count+1}}" value="{{$answer->id}}" id="ans{{$answer->answer}}" />

        <span class="option{{$answer->answer+1}}"></span>

      </label>

    @endforeach

  </div>

 </div>

$("#sub").click(function() {

  var check = true;

  $("input:radio").each(function() {

    var name = $(this).attr('name');

    if ($("input:radio[name=" + name + "]:checked").length) {

      check = true;

    } else {

      check = false;

    }

  });


  if (check) {

    $("#form1").submit();

  } else {

    swal("Oops!", "Please select at least one answer in each question.", "error")

  }

});


隔江千里
浏览 136回答 2
2回答

泛舟湖上清波郎朗

假设有多个问题,正如您在问题下的评论中所述,那么您只需要检查.ans元素总数是否与.ans包含已检查无线电的元素数相匹配,如下所示:$("#sub").click(function() {&nbsp; var $answers = $('.ans');&nbsp; var valid = $answers.length == $answers.filter(':has(:radio:checked)').length;&nbsp; if (valid ) {&nbsp; &nbsp; $("#form1").submit();&nbsp; } else {&nbsp; &nbsp; swal("Oops!", "Please select at least one answer in each question.", "error")&nbsp; }});作为旁注,出于可访问性的原因,我建议您submit在form元素事件下执行此检查,而不是单击按钮。

哆啦的时光机

这是 - 一如既往 - 使用标准的 vanilla Javascript 很容易实现。不需要jQuery。document.forms[0].addEventListener('submit', (event) => {&nbsp; const check =&nbsp; [...document.forms[0].querySelectorAll('fieldset')].every(fieldset => !!fieldset.querySelector('input:checked'));&nbsp; console.log(`${check ? 'A' : 'Not a'}ll questions have been answered!`);&nbsp; // for demo purposes, prevent the submit regardless&nbsp; event.preventDefault();&nbsp; // in your code, you'd do the check&nbsp; // if (!check) event.preventDefault();})form { display: flex; }<form id="questions">&nbsp; &nbsp; <fieldset>&nbsp; &nbsp; &nbsp; <legend>What is 1+1?</legend>&nbsp; &nbsp; &nbsp; <input type="radio" name="addition" id="addition1" value="3" />&nbsp; &nbsp; &nbsp; <label for="addition1">3</label>&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; <input type="radio" name="addition" id="addition2" value="1" />&nbsp; &nbsp; &nbsp; <label for="addition2">1</label>&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; <input type="radio" name="addition" id="addition3" value="2" />&nbsp; &nbsp; &nbsp; <label for="addition3">2</label>&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; </fieldset>&nbsp; &nbsp; <fieldset>&nbsp; &nbsp; &nbsp; <legend>What is 1*1?</legend>&nbsp; &nbsp; &nbsp; <input type="radio" name="multiplication" id="multiplication1" value="3" />&nbsp; &nbsp; &nbsp; <label for="multiplication1">3</label>&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; <input type="radio" name="multiplication" id="multiplication2" value="1" />&nbsp; &nbsp; &nbsp; <label for="multiplication2">1</label>&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; <input type="radio" name="multiplication" id="multiplication3" value="2" />&nbsp; &nbsp; &nbsp; <label for="multiplication3">2</label>&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; </fieldset>&nbsp; &nbsp; <fieldset>&nbsp; &nbsp; &nbsp; &nbsp; <legend>What is 1-1?</legend>&nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="subtraction" id="subtraction1" value="-1" />&nbsp; &nbsp; &nbsp; &nbsp; <label for="subtraction1">-1</label>&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="subtraction" id="subtraction2" value="0" />&nbsp; &nbsp; &nbsp; &nbsp; <label for="subtraction2">0</label>&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; <input type="radio" name="subtraction" id="subtraction3" value="1" />&nbsp; &nbsp; &nbsp; &nbsp; <label for="subtraction3">1</label>&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; </fieldset>&nbsp; &nbsp; &nbsp; <button type="submit">Submit</button>&nbsp; </form>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript