Ver Mensaje Individual
  #12 (permalink)  
Antiguo 07/09/2010, 12:24
Avatar de tredio
tredio
 
Fecha de Ingreso: noviembre-2008
Ubicación: Carabobo
Mensajes: 466
Antigüedad: 15 años, 5 meses
Puntos: 66
Respuesta: Cómo crear una encuesta/quiz inteligente con jquery

sobre la segunda pregunta no te entiendo, que dices que el campo hidden no se borra al actualizar la pagina?, pues eso es exactamente lo que deberia de hacer, si no se te borra no tengo idea de por que esta pasando eso.

sobre la segunda aca te hice algo, pero el script se complico mas, mientras mas le quieras poner mas complicado se pone, aca te dejo lo que hice.

Código Javascript:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/v1.98/DTD/v1.98-strict.dtd">
  2. <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
  3. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  4. <title>Ejemplo de encuesta inteligente</title>
  5.  
  6. <style type="text/css">
  7. #cuestionario {
  8.     padding:10px;
  9.     margin:0px;
  10.     list-style:none;
  11. }
  12. #cuestionario dd {
  13.     display:block;
  14.     height:18px;
  15.     text-decoration:none;
  16.     color:#666666 !important;
  17.     padding:1px 5px 1px 5px;
  18.     border:1px solid #ffffff;
  19. }
  20. #cuestionario dd:hover {
  21.     border:1px solid #e53500;
  22.     text-decoration:none;
  23.     cursor:pointer;
  24. }
  25. .activo{
  26.     background:red;
  27.     font-weight:bold;
  28.     color:#fff;
  29. }
  30. .button{ width:100px; border:solid 1px #CCC; text-align:center; display:none; float:left; margin-right:20px;}
  31. .button:hover{ background:#009; color:#FFF; cursor:pointer;}
  32. </style>
  33.  
  34. <script src="jquery.js" type="text/javascript"></script>
  35. <script type="text/javascript">
  36. $(document).ready(function() {
  37.     var question = 1;
  38.    
  39.     function loadQuestion(param){//CARGA LA SIGUIENTE PREGUNTA
  40.         var r = (param)?(param.respuestas)?'&r='+param.respuestas:'':'';
  41.         $('#cuestionario').html('').load('preguntas.php?q='+(question)+r,(param)?param.callback:null);
  42.     }
  43.    
  44.     function getRespuestas(){
  45.         var respuestas = '';
  46.         $('.activo').each(function(i){//OBTENEMOS LAS RESPUESTAS SELECCIONADAS
  47.             respuestas += $(this).attr('id').split('-')[1];
  48.             if(i!= ($('.activo').length-1))
  49.                 respuestas+=',';
  50.         });
  51.         return respuestas;
  52.     }
  53.    
  54.     function seleccionarRespuestas(){
  55.         var pregunta = getPregunta();
  56.         var val = ($('#answers').val() == '')?'':$('#answers').val()+'|';
  57.        
  58.         var respuestas = getRespuestas();
  59.        
  60.         if(val.indexOf('R['+pregunta+']') != -1){//SI YA SE HAN SELECCIONADO RESPUESTAS PREVIAMENTE PARA ESA PREGUNTA
  61.             var p = val.split('|')[parseInt(pregunta)-1];
  62.             var p2 = p.split(':')[0]+':'+respuestas;
  63.             val = val.replace(p,p2);
  64.         }else{
  65.             val += 'R['+pregunta+']:'+respuestas;
  66.         }
  67.         return {val:val,respuestas:respuestas};
  68.     }
  69.    
  70.     function marcarElegidas(){
  71.         var respuestas = $('#answers').val().split('|')[parseInt(getPregunta())-1].split(':')[1].split(',');//OBTENEMOS LAS RESPUESTAS SELECCIONADAS DE LA PREGUNTA
  72.         for(var i=0;i<respuestas.length;i++){
  73.             $('#r-'+respuestas[i]).addClass('activo');
  74.         }
  75.     }
  76.  
  77.     $('a.empezar_quiz').click(function() {
  78.         loadQuestion()
  79.         $(this).hide();
  80.         $('.button').show();       
  81.     });
  82.    
  83.     $('#next').click(function(){
  84.         if(getN() == $('.activo').length){     
  85.             var r =     seleccionarRespuestas();
  86.             $('#answers').val(r.val);
  87.             question++;
  88.             loadQuestion({respuestas:r.respuestas,callback:marcarElegidas});   
  89.         }else{
  90.             var n = getN();
  91.             var text = (n > 1)?' preguntas':' pregunta';
  92.             alert('Debe seleccionar '+getN()+text);
  93.         }
  94.     });
  95.    
  96.     $('#prev').click(function(){
  97.         if(getPregunta() != 1){
  98.             if(getN() == $('.activo').length){             
  99.                 var r =     seleccionarRespuestas();
  100.                 $('#answers').val(r.val);
  101.                 question--;
  102.                 loadQuestion({respuestas:r.respuestas,callback:marcarElegidas});               
  103.             }else{
  104.                 var n = getN();
  105.                 var text = (n > 1)?' preguntas':' pregunta';
  106.                 alert('Debe seleccionar '+getN()+text);
  107.             }
  108.         }else if(getPregunta() == 1){
  109.             alert('Ya se encuentra en la primera pregunta.')
  110.         }
  111.     });
  112.        
  113.     function getN(){
  114.         return $("#cuestionario dl").attr('class').split('n')[1].split(']')[0].split('[')[1];
  115.     }
  116.    
  117.     function getPregunta(){
  118.         return $("#cuestionario dl").attr('id').split('-')[1];
  119.     }
  120.    
  121.     $("#cuestionario dl dd").live('click', function () {
  122.         if($(this).hasClass('activo') || $('.activo').length < getN()){
  123.             $(this).toggleClass('activo');
  124.             //$("#debug").text("Hay " + $('.activo').length + " elementos seleccionados");
  125.         }
  126.     });
  127.    
  128.     // Una vez empieza el test el boton empezar debe desaparecer
  129.     //$('a.empezar_quiz').hide();
  130. });
  131. </script>
  132.  
  133. </head>
  134. <body>
  135. <a class="empezar_quiz" href="#"> Empezar con las preguntas</a>
  136.  
  137. <!-- Carga las preguntas y respuestas -->
  138. <div id="cuestionario"></div>
  139.  
  140. <!-- Aqui se guardaran las respuestas -->
  141. <input id="answers" name="answers" type="hidden" value="" />
  142.  
  143. <div id="prev" class="button">Anterior</div>
  144. <div id="next" class="button">Continuar</div>
  145.  
  146. <!-- DEBUG: muestra las respuestas seleccionadas -->
  147. <span id="debug"></span>
  148.  
  149. </body>
  150. </html>