【问题标题】:Chrome autofill without submitting form无需提交表单的 Chrome 自动填充
【发布时间】:2017-01-25 06:45:12
【问题描述】:

我已经用 jQuery 替换了我的 <form><input type='submit'>,所以我可以使用 Ajax 发送输入数据,而不是重新加载页面。

这很好用,但是 Chrome 自动填充停止工作,这是我需要的。

我当前代码的简化版本:

<input type="text" id="field1">
<input type="button" id="send" value="Save">

由 jQuery 处理:

$("#send").click(function () {
    $.post('my.php', {
        field1: field1
    });
});

如何让 Google 记住在 field1 中输入的值?

【问题讨论】:

  • 你在使用任何 php 框架吗?如果您使用的是 Codeigniter,那么您可以使用

标签: jquery html ajax google-chrome


【解决方案1】:

您可以使用带有提交按钮的表单,因此 chrome 可以自动填充表单,您可以为未激活 javascript 的浏览器进行后备。当X-Requested-With 标头的值为XMLHttpRequest 时,您可以检查此项。

例如,您可以为 ajax 请求发送 json 响应,为不通过 XHR 请求页面的旧浏览器发送 html 响应。

示例 jQuery 代码:

$("#myForm").on("submit",function(e) {
  if(everythingIsFine) {
     // Send form via ajax to the backend
     sendMyForm();
  } else {
    doSomeWarningsForUser();
  }

  // Prevent browser from submitting form
  e.preventDefault();
}

你的 html 是这样的:

<form method="POST" action="/myAction/" id="myForm">
   <input type="text" id="field1">
   <button type="submit">Save</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-28
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多