javascript – 在单个网页上一次显示调查一个语句的有效方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 在单个网页上一次显示调查一个语句的有效方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6751字,纯文字阅读大概需要10分钟。
内容图文
![javascript – 在单个网页上一次显示调查一个语句的有效方法](/upload/InfoBanner/zyjiaocheng/787/38f7d47351094ababa80a7674795e27f.jpg)
所以我是HTML / CSS和Web开发的新手,我正在设计一个网站来主持基于在线的Likert规模调查.我最初设置了网站,所以当参与者点击链接时,整个调查将显示在页面底部的提交按钮的页面上.但是,我的教授希望它的设计方式一次只显示一个调查声明,用户可以点击“上一个”或“下一个”来浏览调查.
我写了一些代码来完成这个,但对我来说似乎效率极低.必须有一个更好的方法来做我想要完成的事情!
以下是我一直采用的方法示例:
<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>
感谢所有提供帮助的人!
解决方法:
虽然听起来很复杂,但我会简化它:
创建一个数组或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个表.这不是完整的解决方案,只是建议如何节省您的一些时间.
内容总结
以上是互联网集市为您收集整理的javascript – 在单个网页上一次显示调查一个语句的有效方法全部内容,希望文章能够帮你解决javascript – 在单个网页上一次显示调查一个语句的有效方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。