代码之家  ›  专栏  ›  技术社区  ›  Sotiris Adrian J. Moreno

jquery问卷

  •  0
  • Sotiris Adrian J. Moreno  · 技术社区  · 15 年前

    我试图创建一个jquery问卷,但我的jquery知识对我没有帮助。

    到目前为止我有以下几点 http://valogiannis.com/stackoverflow/quest.html

    单击问题时,弹出一个弹出窗口,其中有两个问题。

    我想要以下内容:当用户单击两个答案中的一个时,脚本会检查他的答案。数组checkfirstanswer负责“告诉”第一个答案是否错误或正确,0表示错误,1表示正确。如果用户单击正确的答案,那么我希望显示下一个问题及其来自数组的答案:问题、第一个答案、第二个答案,否则相应的结论与i值成比例。

    如果有任何帮助,我将不胜感激。

    谢谢

    2 回复  |  直到 13 年前
        1
  •  2
  •   gblazex    15 年前

    Working demo

    // question number
    var currentQ = -1;
    
    function showNewQuestion(el) {
        currentQ++; // increment question number
        $('.messagepop').html( Question[currentQ] + '<br />' + 
                              '<a href="#" class="first">' 
                                 + FirstAnswer[currentQ] + '</a><br />' + 
                              '<a href="#" class="second">' 
                                 + SecondAnswer[currentQ] + '</a><br />' + 
                              '<a href="#" class="close">close</a>' );
    }
    
    function validate(answer) {
        var firstIsTrue = CheckFirstAnswer[currentQ];
    
        // correct answer (new question)
        if (  firstIsTrue && answer == 1
          || !firstIsTrue && answer == 2 ) {
          showNewQuestion(); 
        } 
    
        // incorrect answer (conclusion)
        else  {
           $('.messagepop').html(Conclusion[currentQ]);
        }
    }
    

    $(function () {
    
      $("#container_div").live('click', function (event) {
    
        // which element was clicked
        var el = $(event.target);
    
        // first answer was clicked
        if (el.hasClass("first")) {
            validate(1);
        } 
    
        // second answer was clicked
        else if (el.hasClass("second")) { 
            validate(2);
        } 
    
        // questions opener was clicked
        else if (el.attr("id") == "questions") {
            el.addClass("selected").parent()
              .append('<div class="messagepop pop" />');
            showNewQuestion();
            $(".pop").slideFadeToggle();
        } 
    
        // popup close was clicked
        else if (el.hasClass("close")) {
            $(".pop").slideFadeToggle();
            $("#questions").removeClass("selected");
        }
      });
    });
    
        2
  •  0
  •   Ravindra Sane    15 年前