0
$(document).ready(function() {
 var login = $("#login").val();
 var password = $('#password').val();


 $('.login-button').click(function() {
  alert(login);
 });
});

HTML

<form method="post" class="login">  <p id="login-error"></p>
<p>
  <label for="login">Username:</label>
  <input type="text" name="login" id="login" placeholder="username">
</p>

<p>
  <label for="password">Password:</label>
  <input type="password" name="password" id="password" placeholder="password">
</p>

<p class="login-submit">
  <button class="login-button">Login</button>
</p>

<p class="forgot-password">Fill your username and password.</p>   </form>

它什么也没显示!怎么了?有什么解决办法吗?id 名称是正确的

编辑:我还有另一个问题。不想为此提出另一个问题。我正在尝试从 validate.php 中获取 ajax 答案(它位于 /views/admin/validate.php - 它在打开 domain.com/validate 时运行)

$(document).ready(function() {  
$('.login-button').click(function() {
var login = $("#login").val();
var password = $('#password').val();
$.ajax({
  type: 'POST',
  url: '/view/admin/validate.php',
  data: {
  login : login,
  password : password
  },
  success: function(data){
    $('#login-error').html(data);
  }
});
});
});

验证.php

<?php
session_start();
$user = mysql_real_escape_string($_POST['login']);
$password = mysql_real_escape_string(sha1($_POST['password']));
$query = mysql_query("SELECT * FROM  `users` WHERE name =  '$user' AND pass =  '$password' AND privileges = 'superuser'");
$num_rows = mysql_num_rows($query);
if($num_rows == '0') {
echo "Username and Password are incorrect! (Maybe you don't have permission!)";
}
elseif($num_rows == '1') {
$expire = time()*60*60*60*60;
setcookie("user","$user",$expire);
$_SESSION['user'] = $user;
include '/views/admin/admin.php';
}
?>

它应该返回用户名和密码不正确!(也许你没有权限!) - 但它没有..任何解决方案?

4

3 回答 3

5
var login = $("#login").val();

当此行运行时,输入为空。

您需要在用户键入内容后获取该值。

于 2013-06-09T16:07:40.467 回答
0
$('.login-button').click(function() {
  var login = $('#login').val();
  alert(login);
 });
于 2013-06-09T16:08:44.890 回答
0

试试看 :

$('.login-button').click(function() {
  alert($("#login").val());
 });

或者 :

$('.login-button').click(function() {
      var login = $("#login").val();
      alert(login);
     });

或者 :

var login = "";
$('.login-button').click(function() {
      login = $("#login").val();
      alert(login);
     });
于 2013-06-09T16:12:56.640 回答