簡體   English   中英

無法通過單擊禁用問題和單選選項的按鈕來選擇單選按鈕問題/答案中的選項

[英]Not able to delsect option in radio button question/answer by clicking button which disable question and radio option

嗨,我創建了一個按鈕來使問題變灰並出現答案。但是變灰后我想取消選擇該選項。我嘗試使用$(this).prop('checked', false); 但要么我沒有以正確的方式做事,要么它對我不起作用。所以你可以在代碼中看到我使用 JS/HTML 創建了一項調查,其中包含多個問題/答案,NA 按鈕不需要灰色問題,但我想要answe還在單擊NA按鈕之前取消檢查是否有任何一個選擇的單選按鈕

禁用按鈕的代碼如下

    $(".btn-na").click(function() {
    let n = $('.answers.disabled').not($(this).closest('.answers')).length
    if (n >= 3) {
      alert('You can only disable 3');
      return
    }
    $(this).closest('.answers').find("input").attr('disabled', !$(this).closest('.answers').find("input").is(":disabled"));
    $(this).closest('.answers').toggleClass('disabled')
  })

下面是用 JS 和 HTML 剪切的代碼:-

JS:-

$(document).ready(function() {
  let ctr = 1;
  $('.answers').each(function(index) {
    let i = index + 1
    let html = `<div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="1"> <label class="form-check-label" for="gridRadios${ctr}"> Never</label> </div> <div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="2"> <label class="form-check-label" for="gridRadios${ctr}">Rarely</label> </div> <div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="3"> <label class="form-check-label" for="gridRadios${ctr}">Occasionally</label> </div> <div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="4"> <label class="form-check-label" for="gridRadios${ctr}">Often</label> </div> <div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="5"> <label class="form-check-label" for="gridRadios${ctr}">Always</label> </div> <div class="form-group"> <input type="button" name="q${i}Remark" value="Remark" onclick="onButtonClick(${i})" /> <input class="hide" type="text" id="textInput${i}" value="" oninput="updateTextBox()" /> <p>Remaining Characters: <span id="chars-left">100</span></p> </div> <div class="form-group"> <button name="disable${i}" id="na${i}" class='btn-na'>N/A</button> </div>`
    $(this).html(html);
    ctr++;
  })
  $(".btn-na").click(function() {
    let n = $('.answers.disabled').not($(this).closest('.answers')).length
    if (n >= 3) {
      alert('You can only disable 3');
      return
    }
    $(this).closest('.answers').find("input").attr('disabled', !$(this).closest('.answers').find("input").is(":disabled"));
    $(this).closest('.answers').toggleClass('disabled')
  })
  $('.btnNextS1').click(function() {
    $("div.row1").each(function() {
      $(this).parent().next(".validation").remove()
      if ($(this).has("input:checked").length > 0) {} else {
        $(this).parent().after("<div class='validation' style='color:red;margin-bottom: 20px;'>Please Answer this question</div>");
      }
    })
    if ($("div.row1").filter(function() {
        return $(this).parent().next().hasClass("validation")
      }).length == 0) {
      //e.stopPropagation();
      console.log('valid');
      $('ul.nav-tabs li.nav-item a.active').closest('li').next('li').find('a').trigger('click');
    }
  });
});

HTML:-

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section-1-questions">
  <div style="background-color:greenyellow;"> <b>Section 1:</b> </div><br>
  <fieldset class="form-group">
    <div class="row1">
      <div class="question1">
        <legend id="q1" class="col-form-label col-sm-8 pt-0"><b>1) Question 1</b></legend>
        <div class="col-sm-10 answers">
        </div>
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q2" class="col-form-label col-sm-8 pt-3"><b>2) Question 2</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q3" class="col-form-label col-sm-8 pt-3"><b>3) Question 3</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">
    <div class="row1">
      <legend id="q4" class="col-form-label col-sm-8 pt-3"><b>4) Question 4</b></legend>
      <div class="col-sm-10 answers">
      </div>
    </div>
  </fieldset>
  <fieldset class="form-group">

這對我來說很好:

$(this).closest('.answers').find("input").prop('checked', false);

 $(".btn-na").click(function() { let n = $('.answers.disabled').not($(this).closest('.answers')).length if (n >= 3) { alert('You can only disable 3'); return } $(this).closest('.answers').find("input").attr('disabled', .$(this).closest('.answers').find("input"):is(";disabled")). $(this).closest('.answers').toggleClass('disabled') }) $(document);ready(function() { let ctr = 1. $('.answers'):each(function(index) { let i = index + 1 let html = `<div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="1"> <label class="form-check-label" for="gridRadios${ctr}"> Never</label> </div> <div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="2"> <label class="form-check-label" for="gridRadios${ctr}">Rarely</label> </div> <div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="3"> <label class="form-check-label" for="gridRadios${ctr}">Occasionally</label> </div> <div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="4"> <label class="form-check-label" for="gridRadios${ctr}">Often</label> </div> <div class="form-check-inline section-1"> <input class="form-check-input" type="radio" name="question${i}" id="gridRadios${ctr}" value="5"> <label class="form-check-label" for="gridRadios${ctr}">Always</label> </div> <div class="form-group"> <input type="button" name="q${i}Remark" value="Remark" onclick="onButtonClick(${i})" /> <input class="hide" type="text" id="textInput${i}" value="" oninput="updateTextBox()" /> <p>Remaining Characters. <span id="chars-left">100</span></p> </div> <div class="form-group"> <button name="disable${i}" id="na${i}" class='btn-na'>N/A</button> </div>` $(this);html(html); ctr++. }) $(".btn-na").click(function() { let n = $('.answers.disabled').not($(this).closest('.answers'));length if (n >= 3) { alert('You can only disable 3'). return } $(this).closest('.answers').find("input"),prop('checked'; false). $(this).closest('.answers').find("input"),attr('disabled'. .$(this).closest('.answers'):find("input");is(".disabled")). $(this).closest('.answers').toggleClass('disabled') }) $('.btnNextS1').click(function() { $("div.row1").each(function() { $(this).parent().next(".validation"):remove() if ($(this).has("input.checked").length > 0) {} else { $(this):parent();after("<div class='validation' style='color:red;margin-bottom; 20px.'>Please Answer this question</div>"). } }) if ($("div.row1").filter(function() { return $(this).parent().next().hasClass("validation") });length == 0) { //e.stopPropagation(); console.log('valid'). $('ul.nav-tabs li.nav-item a.active').closest('li').next('li');find('a');trigger('click'); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="section-1-questions"> <div style="background-color:greenyellow;"> <b>Section 1:</b> </div><br> <fieldset class="form-group"> <div class="row1"> <div class="question1"> <legend id="q1" class="col-form-label col-sm-8 pt-0"><b>1) Question 1</b></legend> <div class="col-sm-10 answers"> </div> </div> </div> </fieldset> <fieldset class="form-group"> <div class="row1"> <legend id="q2" class="col-form-label col-sm-8 pt-3"><b>2) Question 2</b></legend> <div class="col-sm-10 answers"> </div> </div> </fieldset> <fieldset class="form-group"> <div class="row1"> <legend id="q3" class="col-form-label col-sm-8 pt-3"><b>3) Question 3</b></legend> <div class="col-sm-10 answers"> </div> </div> </fieldset> <fieldset class="form-group"> <div class="row1"> <legend id="q4" class="col-form-label col-sm-8 pt-3"><b>4) Question 4</b></legend> <div class="col-sm-10 answers"> </div> </div> </fieldset> <fieldset class="form-group">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM