【发布时间】: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