1

我有一个我试图通过 AJAX 提交的登录表单。我有一个类似的注册表格,效果很好;但是,return false 语句不会触发。

我的登录表单如下所示:

  <form name="loginForm" id="loginForm" action="userSystem/userSystem.php" method="post">
    <div class="loginTable">
      <div class="loginRow">
        <div class="loginCell"><label for="username">Username:</label></div>
          <div class="loginCell"><input type="text" name=="username" id="loginFormUser"></div>
        </div>
      <div class="loginRow">
        <div class="loginCell"><label for="password">Password:</label></div>
          <div class="loginCell"><input type="password" name="password" id="loginFormPass"></div>
        </div>
      <div class="loginRow">
        <div class="loginCell">&nbsp;</div>
        <div class="loginCell"><input type="submit" name="submit" value="Log In" id="loginFormSubmit"></div>
      </div>
    </div>
  </form>
</section>
<section id="loginBarRegForm">
  <h1>Step Two</h1>
  <form name="regForm" id="regForm" action="usersystem/register.php" method="post">
    <div class="loginTable">
      <div class="loginRow">
        <div class="loginCell"><label for="r_username">Username:</label></div>
        <div class="loginCell"><input type="text" name="r_username" id="r_username"></div>
        <div class="loginCell"><span id="r_usernameFeedback"></span></div>
      </div>
      <div class="loginRow">
        <div class="loginCell"><label for="r_password">Password:</label></div>
        <div class="loginCell"><input type="password" name="r_password" id="r_password"></div>
        <div class="loginCell"><span id="r_passwordFeedback"></span></div>
      </div>
      <div class="loginRow">
        <div class="loginCell"><label for"r_vpassword">Verify Password</label></div>
        <div class="loginCell"><input type="password" name="r_vpassword" id="r_vpassword"></div>
        <div class="loginCell"><span id="r_vpasswordFeedback"></span></div>
        </div>
      <div class="loginRow">
        <div class="loginCell"><label for="email">Email:</label></div>
        <div class="loginCell"><input type="text" name="r_email" id="r_email"></div>
        <div class="loginCell"><span id="r_emailFeedback"></span></div>
        </div>
      <div class="loginRow">
        <div class="loginCell"></div>
        <div class="loginCell"><input type="submit" name="r_submit" value="Register" id="r_submit"></div>
        <div class="loginCell"></div>
        </div>
      </div>
  </form>

以及单击提交按钮时我尝试执行的javascript

$("#loginFormSubmit").click(function() {
  form_data = {
    username: $("#loginFormUser").val(),
    password: $("#loginFormPass").val(),
    operation: "login",
    is_ajax: 1
  }

  $.ajax({
    type: "POST",
    url: "userSystem/userSystem.php",
    data: form_data,
    dataType: json,
    success: function(data) {
      if(data.success) {
        $("#loginBarLoginForm").fadeOut("slow");
        $("#userDetailsUsername").html(data.username);
        $("#userDetailsEmail").html(data.email);
        $("#userDetails").fadeIn('slow');
      } else {
        $("#loginbarLoginForm").fadeOut("slow");
        $("#loginBoxResponseDiv").html(data.message);
        $("#loginBoxResponseFrame").fadeIn("slow");
      }
    }
  });
  return false;
});

到目前为止,这是我开发的最深入的应用程序,但是对于为什么 return false 将在一个实例中起作用,而在另一个实例中不起作用,这只是没有意义。除了数据之外,它们几乎是相同的功能。

感谢您提供任何帮助 :)

编辑:更新代码。

    $("#loginFormSubmit").click(function() {
    console.log("Click Event Firing");
    var form_data = {
        username: $("#loginFormUser").val(),
        password: $("#loginFormPass").val(),
        operation: "login",
        is_ajax: 1
    };
    console.log("variables Assigned " + form_data['username'] + " " + form_data ['password']);

    $.ajax({
        type: "POST",
        url: "userSystem/userSystem.php",
        data: form_data,
        success: function(data) {
            console.log("Ajax Working");
            if(data.success === true) {
            $("#loginBarLoginForm").hide();
                $("#loginBoxResponseDiv").html("<p>You have been loged in " + data.username + "!");
                $("#loginBoxResponseFrame").fadeIn("slow");
            } else {
                $("#loginBarRegForm").hide();
                $("#loginBoxResponseDiv").html(data.message);
                $("#loginBoxResponseFrame").fadeIn("slow");
            }
        }
    });
});

代码现在将返回 JSON 响应,但不会触发成功函数。

4

4 回答 4

3

return false在处理程序内部但在外部ajax以防止提交的默认操作。

$("#loginForm").on('submit',function() {  //attach handler to form
    form_data = {...}                     //form data
    $.ajax({...});                        //fire ajax
    return false;                         //prevent default action
});
于 2012-05-09T04:56:53.523 回答
0

return false 按要求工作,但问题是 Success 方法是一个回调,只要有来自服务器端的成功消息在 ajax 调用上,Jquery 就会隐式调用它,所以当时你没有任何句柄要从中获取值,因此您需要在另一个方法中定义您的逻辑并在此处进行方法调用,因为它是回调,您没有句柄来操作。

是的,如果您想返回$.ajax()调用以返回 false,则可以将 return false 语句删除到成功方法的范围之外。

于 2012-05-09T04:58:23.457 回答
0

只是一个想法,但将输入的类型从“提交”更改为“按钮”。

而且,仅出于调试目的,不要动态分配点击事件。将您的 JS 更改为:

function NamedFunction() {
  form_data = {
    username: $("#loginFormUser").val(),
    password: $("#loginFormPass").val(),
    operation: "login",
    is_ajax: 1
  }

  $.ajax({
    type: "POST",
    url: "userSystem/userSystem.php",
    data: form_data,
    dataType: json,
    success: function(data) {
      if(data.success) {
        $("#loginBarLoginForm").fadeOut("slow");
        $("#userDetailsUsername").html(data.username);
        $("#userDetailsEmail").html(data.email);
        $("#userDetails").fadeIn('slow');
      } else {
        $("#loginbarLoginForm").fadeOut("slow");
        $("#loginBoxResponseDiv").html(data.message);
        $("#loginBoxResponseFrame").fadeIn("slow");
      }
    }
  });
  return false;
}

然后将此添加到您的提交按钮

onclick="NamedFunction(); return false;"

我不建议您保持这种方式,但也许通过这 2 个更改,您可以真正追踪正在发生的事情。

于 2012-05-09T06:45:02.983 回答
0

事实证明,一直以来,我只是在我的解决方案中遗漏了一些引号。我需要更换:

    dataType: json,

和:

    dataType: "json",

一旦我明白了,解决方案就到位了,现在我的用户无需刷新页面就可以登录。我在这个项目上还有很长的路要走,但感谢大家帮助我指明了正确的方向。我也很高兴自己能够找到答案;但是,如果不是因为我从 SO 社区收到了很好的帮助和提示,我永远不会有。你们是百万分之一!

最终的解决方案最终看起来像这样:

    $("#loginFormSubmit").click(function() {
    console.log("Click Event Firing");
    var form_data = {
        username: $("#loginFormUser").val(),
        password: $("#loginFormPass").val(),
        operation: "login",
        is_ajax: 1
    };
    console.log("variables Assigned " + form_data['username'] + " " + form_data ['password']);

    $.ajax({
        type: "POST",
        url: "userSystem/userSystem.php",
        dataType: "json",
        data: form_data,
        success: function(data) {
            console.log("Ajax Working");
            console.log("data.success: " + data.success);
            console.log("data.username: " + data.username); 
            if(data.success === true) {
                console.log("data.success was true");
                $("#loginBarLoginForm").hide();
                $("#loginBoxResponseDiv").html("<p>You have been loged in " + data.username + "!");
                $("#loginBoxResponseFrame").fadeIn("slow");
                $("#notLoggedIn").fadeOut("slow");
                $("#currentlyLoggedIn").fadeIn("slow");
                $.cookie("username", data.username, {expires: 7, path: "/"});
                $.cookie("id", data.id, {expires: 7, path: "/"});
                $.cookie("email", data.email, {expires: 7, path: "/"});
                $.cookie("user_level", data.user_level, {expires: 7, path: "/"});
                $.cookie("active", data.active, {expires: 7, path: "/"});
                $.cookie("reg_date", data.reg_date, {expires: 7, path: "/"});
                $.cookie("last_login", data.last_login, {expires: 7, path: "/"});
                $("#cookieUsername").html($.cookie("username"));
                $("#cookieID").html($.cookie("id"));
                $("#cookieEmail").html($.cookie("email"));
                $("#cookieUserLevel").html($.cookie("user_level"));
                $("#cookieActive").html($.cookie("active"));
                $("#cookieRegDate").html($.cookie("reg_date"));
                $("#cookieLastLogin").html($.cookie("last_login"));
            } else {
                console.log("data.success was false");
                $("#loginBarRegForm").hide();
                $("#loginBoxResponseDiv").html(data.message);
                $("#loginBoxResponseFrame").fadeIn("slow");
            }
        }
    });
});
于 2012-05-09T16:20:56.940 回答