2011-09-26 48 views
1

基本想法是有几个问题,但只有1个问题在页面加载时随机显示。然后,无论回答那个问题,都会显示一个下载。显示下载链接的随机问题

问题随机加载,但是当我放入答案时,下载不会显示。我以为我的代码都是正确的,但也许有更好的方法来做到这一点。如果你能帮助我,我将不胜感激。

P.S.我已经知道这不是一种超安全的隐藏方式,但它并不需要非常安全。

<form id="1"> 
Enter the 5th word on page 28 to access the download: 
<input id="pwd1" name="pwd1" type="text" /> 
</form> 
<form id="2"> 
Enter the 6th word on page 29 to access the download: 
<input id="pwd2" name="pwd2" type="text" /> 
</form> 
<form id="3"> 
Enter the 4th word on page 30 to access the download: 
<input id="pwd3" name="pwd3" type="text" /> 
</form> 
<form id="4"> 
Enter the 3rd word on page 32 to access the download: 
<input id="pwd4" name="pwd4" type="text" /> 
</form> 
<form id="5"> 
Enter the 5th word on page 33 to access the download: 
<input id="pwd5" name="pwd5" type="text" /> 
</form> 
<form id="6"> 
Enter the 3rd word on page 34 to access the download: 
<input id="pwd6" name="pwd6" type="text" /> 
</form> 
<form id="7"> 
Enter the 5th word on page 35 to access the download: 
<input id="pwd7" name="pwd7" type="text" /> 
</form> 
<form id="8"> 
Enter the 4th word on page 36 to access the download: 
<input id="pwd8" name="pwd8" type="text" /> 
</form> 
<form id="9"> 
Enter the 6th word on page 37 to access the download: 
<input id="pwd9" name="pwd9" type="text" /> 
</form> 


<div id="hc" style="display: none;"> 
<center><a href="sudoku.zip"><img id="download" src="download.png"></a></center> 
</div> 

<script language="javascript"> 
var rand = Math.floor(Math.random()*9); 
$('form').hide().eq(rand).show(); 
$('form').keyup(function(){ 
if($('#pwd1').val() == 'more'){ 
    $('#hc').show('slow'); 
} 
if($('#pwd2').val() == 'Rectangle'){ 
    $('#hc').show('slow'); 
} 
if($('#pwd3').val() == 'case'){ 
    $('#hc').show('slow'); 
} 
if($('#pwd4').val() == 'similarities'){ 
    $('#hc').show('slow'); 
} 
if($('#pwd5').val() == 'similar'){ 
    $('#hc').show('slow'); 
} 
if($('#pwd6').val() == 'this'){ 
    $('#hc').show('slow'); 
} 
if($('#pwd7').val() == 'done'){ 
    $('#hc').show('slow'); 
} 
if($('#pwd8').val() == 'outside'){ 
    $('#hc').show('slow'); 
} 
if($('#pwd9').val() == 'completed'){ 
    $('#hc').show('slow'); 
} 
else{$('#hc').hide();} 
}); 
</script> 
+0

你的代码适合我。必须添加对jquery的引用。 –

回答

1

这将是很多清洁:

var rand = Math.floor(Math.random() * 9), 
    words = ['more', 'Rectangle', 'case', 'similarities', 'similar', 'this', 'done', 'outside', 'completed']; 
$('form').hide().eq(rand).show(); 
$('form input').keyup(function() { 
    if (this.value === words[rand]) 
     $('#hc').show('slow'); 
}); 

这里是它的一个jsFiddle工作。

编辑我有words错误的索引,现在修复。

+0

非常感谢乍得 – jaruesink

2

只有当#pwd9是所示的代码时,你的代码才会工作。将您的if更改为else if。或switch

和NO,它根本不安全。链接是一直在那里

0

我认为你在语法中缺少一些东西 - 所有你的if语句(bar第一个),应该是elseif的。

这真的不是很安全,说实话,可能比做'正确'更多的工作(即动态显示问题,只有一种形式,并可能做一些事情(几乎任何事情),使这更安全)。

0

我不确定这可以工作。这是应该怎么做:

  1. 服务器端语言,如PHP,选择随机抽题,并打印出屏幕上的一个形式。

  2. 你的表单需要一个提交按钮,当按下按钮时,它将把数据发送到服务器。

  3. 服务器端语言将处理数据并决定是否显示链接。

这是它是如何的正常进行+它更安全,答案和链接不能这么容易读取。

0

你的问题是你只使用if's。使用else if为每个如果作出最后的相对于所有if而不是最后一个。