简体   繁体   English

使用JavaScript自动登录无法正常工作

[英]Automatic Login Using JavaScript not Working Properly

I would like it such that a user only has to sign-in once and then the next time they open the hybrid app the get signed in automatically. 我希望用户只需要登录一次,然后在下次打开混合应用程序时便会自动登录。 The JavaScript code below here works but only when I remove the 'login/submit' div () which I need. 下面的JavaScript代码有效,但仅当我删除所需的'login / submit'div()时才有效。 How can I get around this? 我该如何解决?

HTML; HTML;

<body>

<form name="EventConfirmRedirection" class="Form" method="post" action="index.php" id="myForm" data-ajax="false">
  <div class="user_login3"><input style="text-transform:lowercase" type="text" name="username" id="username" placeholder="username"></div> 
  <div class="user_login3"><input type="password" name="password" id="password" placeholder="password"></div>

    <div style="margin-left:5%; width:45%; font-size:5px;">
        <input data-theme="c" type="checkbox" id="rememberMe" name="rememberMe"/>
        <label for="rememberMe"><span style="font-size:12px">remember me</span></label>
    </div>   

    <div style="margin-left:5%; color:#FF0000; font-weight:bold" id="error"></div>



    <div class="login"><input type="submit" value="LOGIN" name="submit" data-theme="e" id="submit"></div>
</form>

</body>

JAVASCRIPT; JAVASCRIPT;

$(document).ready(function() {

"use strict";

    if (window.localStorage.checkBoxValidation && window.localStorage.checkBoxValidation !== '') {
        $('#rememberMe').attr('checked', 'checked');
        $('#username').val(window.localStorage.userName);
        $('#password').val(window.localStorage.passWord);
        document.EventConfirmRedirection.submit();
    } else {
        $('#rememberMe').removeAttr('checked');
        $('#username').val('');
        $('#password').val('');
    }

     $('#rememberMe').click(function() {

        if ($('#rememberMe').is(':checked')) {
            // save username and password
            window.localStorage.userName = $('#username').val();
            window.localStorage.passWord = $('#password').val();
            window.localStorage.checkBoxValidation = $('#rememberMe').val();
        } else {
            window.localStorage.userName = '';
            window.localStorage.passWord = '';
            window.localStorage.checkBoxValidation = '';
        }
    });
});

AJAX AJAX

$(document).ready(function() {

"use strict";

    $("#submit").click( function(e) {
    e.preventDefault();

      if( $("#username").val() === "" || $("#password").val() === "" )
       { 
        $("div#error").html("Both username and password are required");
       } else {
                $.post( $("#myForm").attr("action"),
                        $("#myForm :input").serializeArray(),
                        function(data) {
                          $("div#error").html(data);
                        });

                $("#myForm").submit( function() {
                   return false;
                });
               }
});

});

@chiboz in your javascript, instead of using: 在您的JavaScript中使用@chiboz,而不要使用:

document.EventConfirmRedirection.submit();

use: 采用:

$('#submit').click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM