我已经尝试过,但似乎无法弄清楚如何调试它。在用户单击“立即兑换此优惠!”之前,该 div 应该是不可见的。按钮。“面板” div 应该出现在内容之上。如果用户没有在文本字段中输入任何内容,它应该变成红色。
<div id="centreblock">
<h1>WOW! AMAZING!!!</h1>
<img src="images/ipad.gif" width="100%" height="250">
<h2>Congratulations! You won a free iPad! Enter your name, address, and phone number to have it delivered now.</h2>
<form action="">
<label for="firstName">First Name: </label>
<input type="text" size="12" id="firstName" name="firstName" maxlength="30">
<br>
<p id="output"></p>
<input type="button" id="popup" value="REDEEM THIS OFFER NOW!">
<br>
</form>
</div>
<div id="panel">
<h1 id="output-inside"></h1>
</div>
<script src="http//code.jquery.com/jquery-latest.min.js"></script>
<script src="js/script.js"></script>
Javascript
//script.js
var firstName;
function newPanel() {
$('#panel').show();
$('#output-inside').text('Congrats ' + firstName + ', your iPad will be delivered to you in the next century')
}
function panelCheck() {
firstName = $('#firstName').val();
if (firstName != '') {
$('#firstName').removeClass('error');
$('#output').text('');
newPanel();
} else {
$('#firstName').addClass('error').focus();
$('#output').text('We need your name');
}
}
$(function() {
$('#panel').hide();
$('#popup').click(function() {
panelCheck();
});
$('#firstName').keypress(function(e) {
if (e.keyCode === 13) {
panelCheck();
}
});
$('#close-panel').click(function() {
$('#panel').hide();
});
});