You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何在HTML中使用on/off之外的autocomplete属性值?

为什么要使用on/off之外的autocomplete属性值?

我发现表单自动填充只有在name属性和之前存储数据的name完全匹配时才生效(比如输入框name="fname",只有存储的数据里有fname="naresh"才会给出建议);如果没有name属性,就需要id一致。不管autocomplete设为on还是其他非off值,都是这个逻辑,那为什么还要用given-name、family-name、new-password这类具体值呢?

我做了两个测试文件input1.html和input2.html,发现autocomplete好像没起作用,只有name属性相同时自动填充才生效:

input1.html 代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <form>
    <label for="firstname">first name: </label><input type="text" id="firstname" name="fname" autocomplete="given-name">
    <br>
    <label for="lastname">last name: </label><input type="text" id="lastname" name="lname" autocomplete="family-name">
    <input type="submit">
  </form>
</body>

</html>

input2.html 代码:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <form>
    <label for="firstname">first name: </label><input type="text" id="firstname" name="firstname"
      autocomplete="given-name">
    <br>
    <label for="lastname">last name: </label><input type="text" id="lastname" name="lastname"
      autocomplete="family-name">
    <input type="submit">
  </form>
</body>

</html>

我在input1.html里填完表单后打开input2.html,没有自动填充建议;但把input2.html里输入框的name改成fname和lname后,就能得到建议。

autocomplete在这里到底起什么作用?直接用autocomplete=on不就行了,为什么需要其他具体值?我用的是Google Chrome浏览器。


内容的提问来源于stack exchange,提问作者Naresh Lohar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 04:25:10