0

我正在尝试使用 1-5 比例选项创建调查。如果用户选择 1-3 的值,我想让 javascript 显示一个用于评论的文本框。任何人都可以帮我使用javascript代码吗?

这是我的代码:

<li>
  <span class="survey-question">How would you rate your experience with online shopping?</span>
  <div class="survey-answer">
    <label>
          <input class="radio-button" type="radio" name="required" value="1"> Horrible
        </label>
    <label>
        <input class="radio-button" type="radio" name="required" value="2"> Bad
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="3"> So-So
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="4"> Great
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="5" checked> Awesome!
        </label>
  </div>


  <!-- If an answer <= "So-So or less", display: -->
  <div id="improvement" class="new">
    <span class="survey-question">How can we improve?</span>
    <textarea class="col-md-10" rows="3" id="answer" placeholder="Comments..."></textarea>
  </div>
</li>

4

2 回答 2

0

只需要连接一个更改侦听器,然后根据您的条件显示隐藏(我包括 JQuery 只是为了方便,但您可以使用 vanilla,没问题):

根据您的评论,我更新了代码片段以处理多个问题。我不得不删除“id”字段,这样您就没有重复的标识符,并且我通过“.new”类定位了文本框 - 虽然这适用于示例,但使用 .parent().parent 有点僵硬() - 但这足以让您上路。此外,您必须为每组单选按钮的“名称”字段提供不同的标识符。

$(".new").hide();
$("input").on("change",handleChange);
function handleChange(e){
    var rating = $(e.currentTarget).val();
    var $_textBox = $(e.currentTarget).parent().parent().next(".new");
    if(rating < 4 && $_textBox){
        $_textBox.show();
    }else{
        $_textBox.hide();
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li>
  <span class="survey-question">QUESTION 1: How would you rate your experience with online shopping?</span>
  <div class="survey-answer">
    <label>
          <input class="radio-button" type="radio" name="required" value="1"> Horrible
        </label>
    <label>
        <input class="radio-button" type="radio" name="required" value="2"> Bad
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="3"> So-So
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="4"> Great
        </label>
    <label>
          <input class="radio-button" type="radio" name="required" value="5" checked> Awesome!
        </label>
  </div>


  <!-- If an answer <= "So-So or less", display: -->
  <div class="new">
    <span class="survey-question">How can we improve?</span>
    <textarea class="col-md-10" rows="3" id="answer" placeholder="Comments..."></textarea>
  </div>
</li>
<br>
<li>
  <span class="survey-question">QUESTION 2: How would you rate your experience with online shopping?</span>
  <div class="survey-answer">
    <label>
          <input class="radio-button" type="radio" name="required2" value="1"> Horrible
        </label>
    <label>
        <input class="radio-button" type="radio" name="required2" value="2"> Bad
        </label>
    <label>
          <input class="radio-button" type="radio" name="required2" value="3"> So-So
        </label>
    <label>
          <input class="radio-button" type="radio" name="required2" value="4"> Great
        </label>
    <label>
          <input class="radio-button" type="radio" name="required2" value="5" checked> Awesome!
        </label>
  </div>


  <!-- If an answer <= "So-So or less", display: -->
  <div class="new">
    <span class="survey-question">How can we improve?</span>
    <textarea class="col-md-10" rows="3" id="answer" placeholder="Comments..."></textarea>
  </div>
</li>

于 2020-01-28T16:16:41.740 回答
0

使用 onchange 事件onchange="getRating(this)"来捕捉来自无线电输入的选择并从中获取价值。如果值为 1、2 或 3,则会出现改进部分:

var feedbackElem = document.getElementById("improvement")
if (el.value == 3 || el.value == 2 || el.value == 1) { 
  //display improvement}
else { 
  //hide it 
}

function init() {
  var feedbackElem = document.getElementById("improvement")
  feedbackElem.style.display = "none"
}

init ();

function getRating(el) {
  var feedbackElem = document.getElementById("improvement")
  if (el.value == 3 || el.value == 2 || el.value == 1) {
    feedbackElem.style.display = "block"
  } else {
    feedbackElem.style.display = "none"
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<li>
  <span class="survey-question">How would you rate your experience with online shopping?</span>

  <div class="survey-answer">
    <label> 
      <input class="radio-button" type="radio" name="required" value="1" onchange="getRating(this)"> Horrible </label>

    <label> 
      <input class="radio-button" type="radio" name="required" value="2"onchange="getRating(this)"> Bad  </label>

    <label> 
      <input class="radio-button" type="radio" name="required" value="3"onchange="getRating(this)"> So-So  </label>

    <label> 
      <input class="radio-button" type="radio" name="required" value="4"onchange="getRating(this)"> Great </label>

    <label>
    <input class="radio-button" type="radio" name="required" value="5" checked  checkedonchange="getRating(this)"> Awesome! </label>

  </div>


  <!-- If an answer <= "So-So or less", display: -->
  <div id="improvement" class="new">
    <span class="survey-question">How can we improve?</span>
    <textarea class="col-md-10" rows="3" id="answer" placeholder="Comments..."></textarea>
  </div>

</li>

于 2020-01-28T16:17:04.300 回答