在单个网页上一次显示调查一个语句的有效方法

时间:2014-09-24 23:05:55

标签: javascript html web

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

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

以下是我采取的方法的一个示例:

<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>

同样,有一个更好的方法来做到这一点。如果我继续采用这种方法,以后删除/添加或重新组织调查报表将是一件痛苦的事。

非常感谢任何链接或提示!

**** **** SOLUTION 我最终想出了一个使用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>

感谢所有提供帮助的人!!

3 个答案:

答案 0 :(得分: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个表格。这不是完整的解决方案,只是建议如何节省您的一些时间。

答案 1 :(得分:1)

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

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) );
} );

答案 2 :(得分:1)

<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>

再次,这是答案