如果textfield或localstorage包含数据,则自动提交表单

时间:2015-01-30 10:04:05

标签: javascript jquery html local-storage

我想执行自动登录功能。我首次获得了在本地存储中保存电子邮件和密码的脚本,这是用户首次登录时还显示用户在各自文本字段中打开应用程序时的电子邮件和密码(电子邮件和密码)< / p>

$(function() {
  var
    $email = $('#email'),
    $password = $('#password');

  // SAVE VARIABLES TO LOCAL STORAGE
  $('form').on('submit', function() {
    localStorage.setItem("eaddress", $email.val());
    localStorage.setItem("pwd", $password.val());
  });

  // IF EMAIL AND PASSWORD ARE SAVED, PREPOPULATE THE FORM FIELDS
  if (localStorage.getItem("eaddress") != null) {
    $email.val(localStorage.getItem("eaddress"));
  }
  if (localStorage.getItem("pwd") != null) {
    $password.val(localStorage.getItem("pwd"));
  }
});

我的HTML

<form action="login_script.asp" method="post" name="form5" target="_parent" id="form5" onclick="save()" >
            <table width="270" border="0" cellpadding="4" cellspacing="4">
              <tr>
                <td><strong>Email</strong></td>
                </tr>
              <tr>
                <td>

                  <input name="email" type="email" id="email" size="40"/></td>
                </tr>
              <tr>
                <td><strong>Password</strong></td>
                </tr>
              <tr>
                <td>
                  <input name="password" type="password" id="password" size="40"/></td>
                </tr>
              <tr>
                <td>&nbsp;

                  <input type="submit" id="submit" name="submit" value="Submit">
                  &nbsp;</td>
                </tr>
              <tr>
                <td><span style="width: 480px; margin: 0 auto; margin-top: 30px;"><img src="imgs/loader.gif" alt="" name="gif" id="gif" style="display: block; margin: 0 auto; width: 30px; visibility: hidden;" /></span></td>
                </tr>
              </table>

            </form>

<script type="text/javascript">
  $('#form5').submit(function() {
    $('#gif').css('visibility', 'visible');
    return true;
});
            </script>

现在我想要实现的是,一旦localstorage中有数据或者电子邮件和密码文本字段包含一些变量,就应该有自动提交

我得到了一份自动提交表格的研究报告,但不知道如何将其融入我的

$(function(){

$("input[name=name]").val("somename");
$("input[name=email]").val("323@ds.com");
$('#aweberform').submit();

});

<input type="submit" name="someothername" value="click to submit!">

1 个答案:

答案 0 :(得分:0)

这样的事情应该有效:

$(function() {
  var
    $email = $('#email'),
    $password = $('#password'),
    localEmail = localStorage.getItem("eaddress"),
    localPwd = localStorage.getItem("pwd");

  // IF EMAIL AND PASSWORD ARE SAVED PREVIOUSLY, POPULATE THE FORM FIELDS THEN SUBMIT FORM

  // check if email saved previously
  if (localEmail != null) {        
    $email.val(localEmail);
  }

  // check if password saved previously
  if (localPwd != null) {
    $password.val(localPwd);
  }

  // if both exist in localstorage, submit form
  if (localPwd != null && localPwd != null) {
     $('form').submit();
  }

  // SAVE VARIABLES TO LOCAL STORAGE
  $('form').on('submit', function() {
    localStorage.setItem("eaddress", $email.val());
    localStorage.setItem("pwd", $password.val());
  });

});