我正在尝试使用jQuery创建虚假登录页面 - 纯粹用于测试/模板演示目的.
应该采用什么形式(请参阅.js代码中的详细信息):
正确的电子邮件:admin@admin.com/pass:admin
jsfiddle的代码http://jsfiddle.net/MrTest/ZYZPY/
HTML
<form class="loginbox" autocomplete="off">
<fieldset>
<p style="display: none;" class="error">Error. Please enter correct email & password.</p>
<label for="username">E-mail</label>
<input type="text" id="username" />
<label for="username">Password</label>
<input type="password" id="password" />
<input type="submit" id="submit" value="LOG IN" />
</fieldset>
</form>
Run Code Online (Sandbox Code Playgroud)
jQuery的
$(document).ready(function() {
$('#username').focus();
$('#submit').click(function() {
event.preventDefault(); // prevent PageReLoad
$('.error').css('display', 'none'); // hide error msg
var ValEmail = $('#username').val('admin@admin.com'); // Email Value
var ValPassword = $('#password').val('admin'); // Password Value
if (ValEmail === true & ValPassword === true) { // if ValEmail & ValPass are as above
alert('valid!'); // alert valid!
window.location = "home.html"; // go to home.html
}
else {
alert('not valid!'); // alert not valid!
$('.error').css('display', 'block'); // show error msg
}
});
});
Run Code Online (Sandbox Code Playgroud)
目前表格总是返回else = false - 为什么?
任何帮助非常感谢!
皮特
if (ValEmail === true & ValPassword === true) {
Run Code Online (Sandbox Code Playgroud)
应该&&代替&
And val用来获取输入值,你应该:
var ValidEmail = $('#username').val() === 'admin@admin.com'; // Email validate
var ValidPassword = $('#password').val() === 'admin'; // Password validate
if (ValidEmail === true && ValidPassword === true) { // if ValidEmail & ValidPassword
Run Code Online (Sandbox Code Playgroud)
一个工作小提琴:http://jsfiddle.net/DTGt9/