jQuery和AJAX只工作一次

时间:2017-09-27 18:35:59

标签: javascript php jquery ajax

我遇到了AJAX和jQuery的问题。我写了登录系统的功能,但它只是第一次工作。这是我的代码:

模态中的html:

<form role="form" onsubmit=" return login()"  method="post" action="" >
   <div class="form-group">
     <label for="userName"><span class="glyphicon glyphicon-user"></span>E-mail</label>
     <input type="email" name="emailLogin" class="form-control" id="userName" placeholder="e-mail" required>
   </div>
   <div class="form-group">
      <label for="password"><span class="glyphicon glyphicon-eye-open"><span>Password</label>
      <input type="password" name="passLogin" class="form-control" id="password" placeholder="Password" required>
    </div>
    <button type="submit" class="btn btn-success btn-block">Login<span class="glyphicon glyphicon-log-in"></span></button>
</form>

这里是jquery:

function login(){
  login=document.getElementById('userName').value;
  pass=document.getElementById('password').value;
  var dataString="emailLogin="+login+"&passLogin="+pass;

  $.ajax({
    type: "POST",
    url: "models/handler/KlientHandler.php",
    cache: false,
    data: dataString,
    success: function(text){
      if(text=='0'){
        $("#loginError").removeClass('hidden');
      }else{
        $("#loginOk").removeClass('hidden');
        $("#myModal").modal('hide');
        $("#loginLi").html("<a id=\"user\">"+login+" (Profile)<span class=\"glyphicon glyphicon-user\"></span></a>");
        $("#regLi").html("<a href=\"logout\" id=\"user\">"+login+" (Logout)<span class=\"glyphicon glyphicon-log-out\"></span></a>");
      }
    }
  });
  return false;
}

2 个答案:

答案 0 :(得分:2)

您正在使用字符串覆盖登录功能。

<a onclick="foo()">
click me!
</a>
var

查看此处的第二次点击如何导致不同的警报?在您的情况下,您使用字符串而不是函数替换函数,从而导致语法错误。不要忘记var login=document.getElementById('userName').value; var pass=document.getElementById('password').value; 你的变量。

{{1}}

答案 1 :(得分:0)

该代码段会测试您的逻辑,并且您的登录和传递变量存在控制台错误。我将它们设置为&#39; var&#39;并将你的成功逻辑放在“错误”中ajax请求的一部分,因为我无法访问您的服务器,但它确实创建了注销按钮。那是你想要的吗?

&#13;
&#13;
function login() {
    var login = document.getElementById('userName').value;
    var pass = document.getElementById('password').value;
    var dataString = "emailLogin=" + login + "&passLogin=" + pass;

    $.ajax({
        type: "POST",
        url: "models/handler/KlientHandler.php",
        cache: false,
        data: dataString,
        success: function(text) {
          /* Success logic */
        },
        error: function() {
                alert('Test - success logic');
                $("#loginOk").removeClass('hidden');
                //$("#myModal").modal('hide');
                $("#loginLi").html("<a id=\"user\">" + login + " (Profile)<span class=\"glyphicon glyphicon-user\"></span></a>");
                $("#regLi").html("<a href=\"logout\" id=\"user\">" + login + " (Logout)<span class=\"glyphicon glyphicon-log-out\"></span></a>");
        }

    });
    return false;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form role="form" onsubmit=" return login()"  method="post" action="" >
   <div class="form-group">
     <label for="userName"><span class="glyphicon glyphicon-user"></span>E-mail</label>
     <input type="email" name="emailLogin" class="form-control" id="userName" placeholder="e-mail" required>
   </div>
   <div class="form-group">
      <label for="password"><span class="glyphicon glyphicon-eye-open"><span>Password</label>
      <input type="password" name="passLogin" class="form-control" id="password" placeholder="Password" required>
    </div>
    <button type="submit" class="btn btn-success btn-block">Login<span class="glyphicon glyphicon-log-in"></span></button>
</form>

<div id="loginLi"></div>
<div id="regLi"></div>
<div id="loginOk">Login OK</div>
&#13;
&#13;
&#13;