在没有单选按钮的情况下禁用下一个按钮

发布于 2025-02-02 19:34:41 字数 2292 浏览 1 评论 0原文

在我的HTML网站上,我有几个问题,每个问题都有单个问题作为答案,我希望用户只有在选择该问题的任何广播按钮时才能继续下一个问题,我执行了以下代码:

$('div.question').hide().first().show();


$('a.display').on('click', function(e) {

  e.preventDefault();


  var that = $('div.question:visible'),

    t = $(this).text();


  if (t == 'next' && that.next('div.question').length > 0) {
    if ($('input[type=radio]:checked').length > 0) {
      $('div.question').hide();


      that.next('div.question').show()
    } else {
      alert("Please Select an Option !");
    }
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<div class="question bg-white p-3 border-bottom">
  <div class="d-flex flex-row  question-title">
    <h3 class="text-danger">1. </h3>
    <h5 class="mt-1 ml-2">I try to be with people.</h5>
  </div>

  <div class="ans ml-2">
    <div class="form-check">
      <input class="form-check-input" name="q1" value="1" type="radio">
      <input class="form-check-input" name="q1" type="radio" value="2">
      <input class="form-check-input" name="q1" type="radio" value="3">
      <input class="form-check-input" name="q1" type="radio" value="4">
    </div>
  </div>
</div>


<div class="question bg-white p-3 border-bottom">
  <div class="d-flex flex-row  question-title">
    <h3 class="text-danger">1. </h3>
    <h5 class="mt-1 ml-2">I am sincere with people.</h5>
  </div>

  <div class="ans ml-2">
    <div class="form-check">
      <input class="form-check-input" name="q2" value="1" type="radio">
      <input class="form-check-input" name="q2" type="radio" value="2">
      <input class="form-check-input" name="q2" type="radio" value="3">
      <input class="form-check-input" name="q2" type="radio" value="4">
    </div>
  </div>
</div>

<a id="display" class="btn btn-info btn-sm display si">next</a>

在这里,这仅适用于第一个问题,在第一个问题之后,用户能够单击“下一个”按钮而无需检查单选按钮,任何人都可以告诉我如何解决此问题,谢谢

in my html website i have few questions where each questions have radio buttons as answers, i want the user to proceed to next question only when he selects any radio button of the question, i did the following code:

$('div.question').hide().first().show();


$('a.display').on('click', function(e) {

  e.preventDefault();


  var that = $('div.question:visible'),

    t = $(this).text();


  if (t == 'next' && that.next('div.question').length > 0) {
    if ($('input[type=radio]:checked').length > 0) {
      $('div.question').hide();


      that.next('div.question').show()
    } else {
      alert("Please Select an Option !");
    }
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<div class="question bg-white p-3 border-bottom">
  <div class="d-flex flex-row  question-title">
    <h3 class="text-danger">1. </h3>
    <h5 class="mt-1 ml-2">I try to be with people.</h5>
  </div>

  <div class="ans ml-2">
    <div class="form-check">
      <input class="form-check-input" name="q1" value="1" type="radio">
      <input class="form-check-input" name="q1" type="radio" value="2">
      <input class="form-check-input" name="q1" type="radio" value="3">
      <input class="form-check-input" name="q1" type="radio" value="4">
    </div>
  </div>
</div>


<div class="question bg-white p-3 border-bottom">
  <div class="d-flex flex-row  question-title">
    <h3 class="text-danger">1. </h3>
    <h5 class="mt-1 ml-2">I am sincere with people.</h5>
  </div>

  <div class="ans ml-2">
    <div class="form-check">
      <input class="form-check-input" name="q2" value="1" type="radio">
      <input class="form-check-input" name="q2" type="radio" value="2">
      <input class="form-check-input" name="q2" type="radio" value="3">
      <input class="form-check-input" name="q2" type="radio" value="4">
    </div>
  </div>
</div>

<a id="display" class="btn btn-info btn-sm display si">next</a>

here this works only for 1st question, after the 1st question, user is able to click next button without checking radio button, can anyone please tell me how to fix this, thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-02-09 19:34:41

您的片段中只有两个问题,我添加了测试,发现您需要检查可见问题的收音机,以测试您是否可以在下

一个问题上隐藏下一个问题

const $questions = $('div.question').hide().first().show();
const $sub = $("#sub");
const $next = $('#display')
  .on('click', function(e) {
    e.preventDefault();
    var that = $('div.question:visible'),
      t = $(this).text(),
      $nextQuestion = that.next('div.question'),
      moreQuestions = $nextQuestion.length > 0;
    if (t == 'next' && moreQuestions) {
      if ($('input[type=radio]:checked', that).length > 0) {
        $('div.question').hide();
        $nextQuestion.show()
      } else {
        alert("Please Select an Option !");
      }
    }
    const last = $nextQuestion.index() === $questions.length
    $next.toggle(last);
    $sub.toggle(!last);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="questions">
  <div class="question bg-white p-3 border-bottom">
    <div class="d-flex flex-row  question-title">
      <h3 class="text-danger">1. </h3>
      <h5 class="mt-1 ml-2">I try to be with people.</h5>
    </div>

    <div class="ans ml-2">
      <div class="form-check">
        <input class="form-check-input" name="q1" value="1" type="radio">
        <input class="form-check-input" name="q1" type="radio" value="2">
        <input class="form-check-input" name="q1" type="radio" value="3">
        <input class="form-check-input" name="q1" type="radio" value="4">
      </div>
    </div>
  </div>


  <div class="question bg-white p-3 border-bottom">
    <div class="d-flex flex-row  question-title">
      <h3 class="text-danger">1. </h3>
      <h5 class="mt-1 ml-2">I am sincere with people.</h5>
    </div>

    <div class="ans ml-2">
      <div class="form-check">
        <input class="form-check-input" name="q2" value="1" type="radio">
        <input class="form-check-input" name="q2" type="radio" value="2">
        <input class="form-check-input" name="q2" type="radio" value="3">
        <input class="form-check-input" name="q2" type="radio" value="4">
      </div>
    </div>
  </div>

  <div class="question bg-white p-3 border-bottom">
    <div class="d-flex flex-row  question-title">
      <h3 class="text-danger">1. </h3>
      <h5 class="mt-1 ml-2">I am fed up with people.</h5>
    </div>

    <div class="ans ml-2">
      <div class="form-check">
        <input class="form-check-input" name="q2" value="1" type="radio">
        <input class="form-check-input" name="q2" type="radio" value="2">
        <input class="form-check-input" name="q2" type="radio" value="3">
        <input class="form-check-input" name="q2" type="radio" value="4">
      </div>
    </div>
  </div>
</div>
<a id="display" class="btn btn-info btn-sm display si">next</a>
<button type="submit" id="sub" hidden>Submit</button>

You only have two questions in your snippet, I added on to test and found that you need to check the radios of the visible question to test if you can go next

Hiding the next on last question

const $questions = $('div.question').hide().first().show();
const $sub = $("#sub");
const $next = $('#display')
  .on('click', function(e) {
    e.preventDefault();
    var that = $('div.question:visible'),
      t = $(this).text(),
      $nextQuestion = that.next('div.question'),
      moreQuestions = $nextQuestion.length > 0;
    if (t == 'next' && moreQuestions) {
      if ($('input[type=radio]:checked', that).length > 0) {
        $('div.question').hide();
        $nextQuestion.show()
      } else {
        alert("Please Select an Option !");
      }
    }
    const last = $nextQuestion.index() === $questions.length
    $next.toggle(last);
    $sub.toggle(!last);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="questions">
  <div class="question bg-white p-3 border-bottom">
    <div class="d-flex flex-row  question-title">
      <h3 class="text-danger">1. </h3>
      <h5 class="mt-1 ml-2">I try to be with people.</h5>
    </div>

    <div class="ans ml-2">
      <div class="form-check">
        <input class="form-check-input" name="q1" value="1" type="radio">
        <input class="form-check-input" name="q1" type="radio" value="2">
        <input class="form-check-input" name="q1" type="radio" value="3">
        <input class="form-check-input" name="q1" type="radio" value="4">
      </div>
    </div>
  </div>


  <div class="question bg-white p-3 border-bottom">
    <div class="d-flex flex-row  question-title">
      <h3 class="text-danger">1. </h3>
      <h5 class="mt-1 ml-2">I am sincere with people.</h5>
    </div>

    <div class="ans ml-2">
      <div class="form-check">
        <input class="form-check-input" name="q2" value="1" type="radio">
        <input class="form-check-input" name="q2" type="radio" value="2">
        <input class="form-check-input" name="q2" type="radio" value="3">
        <input class="form-check-input" name="q2" type="radio" value="4">
      </div>
    </div>
  </div>

  <div class="question bg-white p-3 border-bottom">
    <div class="d-flex flex-row  question-title">
      <h3 class="text-danger">1. </h3>
      <h5 class="mt-1 ml-2">I am fed up with people.</h5>
    </div>

    <div class="ans ml-2">
      <div class="form-check">
        <input class="form-check-input" name="q2" value="1" type="radio">
        <input class="form-check-input" name="q2" type="radio" value="2">
        <input class="form-check-input" name="q2" type="radio" value="3">
        <input class="form-check-input" name="q2" type="radio" value="4">
      </div>
    </div>
  </div>
</div>
<a id="display" class="btn btn-info btn-sm display si">next</a>
<button type="submit" id="sub" hidden>Submit</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文