Py学习  »  Jquery

jquery无法在测试结束时触发Ajax调用

conmen • 4 年前 • 296 次点击  

alert()

var answers = [];

function nextQuestion() {
  var cq = $('.current.question');
  cq.removeClass('current');
  var hasNext = cq.next().length > 0;
  if (hasNext) {
    cq.next().addClass('current');
  } else {
    alert('you\'ve done!');
    $('body').append('<h2>Quiz completed</h2>');
  }
  return hasNext;
}

$('.question a').on('click', function(e) {
  e.preventDefault();
  var self = $(this);
  var ans = self.parent().index() + 1;
  answers.push(ans);
  var hasNext = nextQuestion();
  if (!hasNext) {
    alert('ajax send data to php');
    //will do ajax call here
  }
});
.question {
  display: none;
}

.question.current {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="question current">First question ?
  <ul>
    <li><a href="#">Answer A</a></li>
    <li><a href="#">Answer B</a></li>
    <li><a href="#">Answer C</a></li>
    <li><a href="#">Answer D</a></li>
  </ul>
</div>
<div class="question">Second Question ?
  <ul>
    <li><a href="#">Answer A</a></li>
    <li><a href="#">Answer B</a></li>
    <li><a href="#">Answer C</a></li>
    <li><a href="#">Answer D</a></li>
  </ul>
</div>
<div class="question">Third question ?
  <ul>
    <li><a href="#">Answer A</a></li>
    <li><a href="#">Answer B</a></li>
    <li><a href="#">Answer C</a></li>
    <li><a href="#">Answer D</a></li>
  </ul>
</div>
<div class="question">Last question ?
  <ul>
    <li><a href="#">Answer A</a></li>
    <li><a href="#">Answer B</a></li>
    <li><a href="#">Answer C</a></li>
    <li><a href="#">Answer D</a></li>
  </ul>
</div>

<!-- <script>/* here is where the script tag would be */</script> -->

我希望当最后一个问题被点击时,一些附加消息也会显示并触发Ajax调用。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/35555
 
296 次点击  
文章 [ 2 ]  |  最新文章 4 年前
Grigor Danielyan
Reply   •   1 楼
Grigor Danielyan    4 年前

H77
Reply   •   2 楼
H77    4 年前

.question

var hasNext = cq.next(".question").length > 0;