【问题标题】:Efficient way to display a survey one statement at a time on single webpage在单个网页上一次显示一项调查的有效方法
【发布时间】:2014-09-24 23:05:54
【问题描述】:

所以我是 HTML/CSS 和 Web 开发的新手,我正在设计一个网站来举办基于在线的李克特规模调查。我最初设置了站点,因此当参与者单击链接时,整个调查将显示在页面上,页面底部的提交按钮。但是,我的教授希望它的设计方式是一次只显示一个调查声明,并且用户可以点击“上一个”或“下一个”来浏览调查。

我已经编写了一些代码来实现这一点,但它对我来说似乎非常低效。必须有更好的方法来完成我想要完成的事情!

这是我一直采用的方法的一个示例:

<html>
<body>
<script type="text/javascript">
function showNext(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";
    document.getElementById(divIdHide).style.display="none";
    }
function showPrevious(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";
    document.getElementById(divIdHide).style.display="none";
    }
</script>

    <div id="div1" class="center">
    <table align="center" style="font-size: 14px"  bgcolor="CCCCCC">
    <tr><td>A survey statement goes here<td></tr></table>
    <table align="center" style="font-size: 14px"  bgcolor="CCCCCC">
        <tr><td><input type="radio" name="s1" value="1">Strongly Disagree  (1)</td>
            <td><input type="radio" name="s1" value="2">Disagree (2)</td>
            <td><input type="radio" name="s1" value="3">Neutral (3)</td>
            <td><input type="radio" name="s1" value="4">Agree (4)</td>
            <td><input type="radio" name="s1" value="5">Strongly Agree (5)</td>
            <td><input type="radio" name="s1" value="0" checked><i>not answered</i></td>
        </tr>
    </table><br>
    <button type="button" disabled>Previous</button>
    <button type="button" onclick="showNext('div2','div1')">Next</button>
    </div>

    <div id="div2" class="center" style="display:none">
    <table align="center" id="t1" style="font-size: 14px"  bgcolor="CCCCCC">
    <tr>Another statement goes here!</tr>
        <tr><td><input type="radio" name="s2" value="1">Strongly Disagree  (1)</td>
            <td><input type="radio" name="s2" value="2">Disagree (2)</td>
            <td><input type="radio" name="s2" value="3">Neutral (3)</td>
            <td><input type="radio" name="s2" value="4">Agree (4)</td>
            <td><input type="radio" name="s2" value="5">Strongly Agree (5)</td>
            <td><input type="radio" name="s2" value="0" checked><i>not answered</i></td>
        </tr>
    </table><br>
    <button type="button" onclick="showPrevious('div1','div2')">Previous</button>
    <button type="button" onclick="showNext('div2','div1')">Next</button>
    </div>
</body>
</html>

同样,必须有更好的方法来做到这一点。如果我继续采用这种方法,那么以后删除/添加或重新组织调查报表将会非常痛苦。

任何链接或提示将不胜感激!

****解决方案**** 我最终想出了一个在 php 中使用循环/回声的解决方案。这是我用来完成任务的代码的粗略(不漂亮)示例:

<html>
<body>

<script type="text/javascript">
function go(){
document.getElementById('state0').style.display = "block";
document.getElementById('state0').scrollIntoView();
}
function showNext(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";

    document.getElementById(divIdHide).style.display="none";
    }
function showPrevious(divIdShow,divIdHide){
    document.getElementById(divIdShow).style.display="block";

    document.getElementById(divIdHide).style.display="none";
    }
</script>


<button type='button' onclick='go()'>GO</button>

<!--<button type='button' onclick="go()">go</button>-->
    <?php
    $numElements=5;
    $array = array("This is the first statement", "This is the second statement","This is the third statement","This is the fourth Statement", "Last statement here");

    for($i=0;$i<$numElements;$i++){

    $divId='state'.$i; 
    $sId='q'.$i;
    $nextInt=$i+1;
    $prevInt=$i-1;
    $nextDiv='state'.$nextInt;
    $curDiv='state'.$i;
    $prevDiv='state'.$prevInt;
    if($i==0){
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' disabled>Previous</button>
    <button type='button' onclick='showNext(\"$nextDiv\",\"$curDiv\")'>Next</button>
    </div>";}
    else if($i==$numElements-1){
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' onclick='showPrevious(\"$prevDiv\",\"$curDiv\")'>Previous</button>
    <button type='button'  disabled>Next</button>
    </div>";}
    else{
    echo "<div id='$divId' class='center' style='display:none'>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
    <tr><td>$array[$i]<td></tr></table>
    <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
        <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
            <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
            <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
            <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
            <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
            <td><input type='radio' name='$sId'  value='6'>No answer  </td>
        </tr>
    </table><br>
    <button type='button' onclick='showPrevious(\"$prevDiv\",\"$curDiv\")'>Previous</button>
    <button type='button' onclick='showNext(\"$nextDiv\",\"$curDiv\")'>Next</button>
    </div>";    }

    }


    ?>

</body>
</html>

感谢所有提供帮助的人!

【问题讨论】:

  • 你可以使用jQuery吗?
  • 是的,我可以使用任何必要的方式进行调查,但我以前从未使用过 jQuery。
  • 你想让代码的哪一部分变小?
  • 好吧,如果你注意到的话,我为调查中的每个陈述都有一个单独的 div。此外,每个
    都有 2 个按钮,它们使用我硬编码的参数调用 javascript 函数。如果我有 50 个问题的调查,我的 html 文件将是巨大的!此外,如果我继续采用这种方法,从调查中删除一个陈述将是一件非常痛苦的事情。
  • 哦,我明白你的意思了。问题的一般形式都一样吗? (问题和 1 到 5 个同意/不同意选项)

标签: javascript html web


【解决方案1】:

虽然听起来很复杂,但我会这样简化:

创建一个包含所有问题的数组或 json 作为键值对 (例如问题= [[1,“你喜欢xy吗?”],[2,“第二个问题],...], 所有问题只有一个表,并使用最初设置的函数 数组的第一个条目作为第一个问题。单击上一个/下一个时调用此函数 浏览问题并将值保存在另一个数组中,存储为 表上的数据属性。
就像一个小演示 Fiddle 来演示它是如何工作的。

var questions = [[1, "how are you"],[2, "next one"],[3, "question number 3"]];

setFirstQuestion();

function setFirstQuestion(){
  $(".question").text(questions[0][1]);
  $(".question").data("current",[0][0]);
}

$("#next").click(function(){
   setNextQuestion();
});

function setNextQuestion()
{
  var current = $(".question").data("current");
  $(".question").text(questions[current + 1][1]);
  $(".question").data("current", current + 1);
}

这可以在您的表格中设置问题,因此您只需使用 50 个表格,而不是 50 个表格。这不是完整的解决方案,而只是一个建议,可以节省您的一些时间。

【讨论】:

    【解决方案2】:

    将您的内容存储为数据结构。即时生成调查。

    function MakeQuestion(Q) {
        var lq = document.createElement('div');
        /* ... */
        return lq;
    }
    
    var Questions = [ { q: "Ham?", a: ['yes', 'double-yes', 'super-yes', 'bacon too'] }, ... ];
    
    Questions.map(function(Q) {
        document.getElementById('qContainer').appendChild( MakeQuestion(Q) );
    } );
    

    【讨论】:

      【解决方案3】:
      <html>
      <body>
      
      <script type="text/javascript">
      function go(){
      document.getElementById('state0').style.display = "block";
      document.getElementById('state0').scrollIntoView();
      }
      function showNext(divIdShow,divIdHide){
          document.getElementById(divIdShow).style.display="block";
      
          document.getElementById(divIdHide).style.display="none";
          }
      function showPrevious(divIdShow,divIdHide){
          document.getElementById(divIdShow).style.display="block";
      
          document.getElementById(divIdHide).style.display="none";
          }
      </script>
      
      
      <button type='button' onclick='go()'>GO</button>
      
      <!--<button type='button' onclick="go()">go</button>-->
          <?php
          $numElements=5;
          $array = array("This is the first statement", "This is the second statement","This is the third statement","This is the fourth Statement", "Last statement here");
      
          for($i=0;$i<$numElements;$i++){
      
          $divId='state'.$i; 
          $sId='q'.$i;
          $nextInt=$i+1;
          $prevInt=$i-1;
          $nextDiv='state'.$nextInt;
          $curDiv='state'.$i;
          $prevDiv='state'.$prevInt;
          if($i==0){
          echo "<div id='$divId' class='center' style='display:none'>
          <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
          <tr><td>$array[$i]<td></tr></table>
          <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
              <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
                  <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
                  <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
                  <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
                  <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
                  <td><input type='radio' name='$sId'  value='6'>No answer  </td>
              </tr>
          </table><br>
          <button type='button' disabled>Previous</button>
          <button type='button' onclick='showNext(\"$nextDiv\",\"$curDiv\")'>Next</button>
          </div>";}
          else if($i==$numElements-1){
          echo "<div id='$divId' class='center' style='display:none'>
          <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
          <tr><td>$array[$i]<td></tr></table>
          <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
              <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
                  <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
                  <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
                  <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
                  <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
                  <td><input type='radio' name='$sId'  value='6'>No answer  </td>
              </tr>
          </table><br>
          <button type='button' onclick='showPrevious(\"$prevDiv\",\"$curDiv\")'>Previous</button>
          <button type='button'  disabled>Next</button>
          </div>";}
          else{
          echo "<div id='$divId' class='center' style='display:none'>
          <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
          <tr><td>$array[$i]<td></tr></table>
          <table align='center' style='font-size: 14px'  bgcolor='CCCCCC'>
              <tr><td><input type='radio' name='$sId'  value='1'>Strongly Disagree  (1)</td>
                  <td><input type='radio' name='$sId'  value='2'>Disagree  (2)</td>
                  <td><input type='radio' name='$sId'  value='3'>Neutral  (3)</td>
                  <td><input type='radio' name='$sId'  value='4'>Agree  (4)</td>
                  <td><input type='radio' name='$sId'  value='5'>Strongly Agree  (5)</td>
                  <td><input type='radio' name='$sId'  value='6'>No answer  </td>
              </tr>
          </table><br>
          <button type='button' onclick='showPrevious(\"$prevDiv\",\"$curDiv\")'>Previous</button>
          <button type='button' onclick='showNext(\"$nextDiv\",\"$curDiv\")'>Next</button>
          </div>";    }
      
          }
      
      
          ?>
      
      </body>
      </html>
      

      再次,这是答案

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签