我有以下内容,我首先要验证用户是否输入了一个值,其次该数值是否与通过 php.ini 生成的 1-8 中的随机数匹配。
HTML / PHP
<?php
$num = rand(1,8);
echo '<script type="text/javascript">
var js_var = '.json_encode($num).';
</script>';
?>
<p>The ball will start at position '<span class="number"></span>' and travel in a straight line.</p>
<p class="question">Where will the ball roll to?</p>
<form action="javascript:alert('You are correct');" id="form">
<input type="text" id="field">
<input type="submit" value="submit">
</form>
<p id="target"></p>
JS
<script type="text/javascript">
$(function(){
// add in random start position
$('.number').text(js_var);
// grab submitted answer
$('#form').submit(function(event){
var answer = $('#field').val();
if( $('#answer').val() != ' ') {
if ( answer == js_var) {
$('#target').text("Correct!").show();
return;
}
$('#target').text("Incorrect, try again!"+js_var).show().fadeOut(1000);
event.preventDefault();
} else {
alert('Please enter a value...!');
event.preventDefault();
}
})
});
</script>
我根本无法让它验证....