最初の二項目から入力を進める
名前と会社名から、登録フォームの続きの項目を推定する実験。自分で決める欄と、確認だけで進める欄の分け方に注目。
入力済みの情報から候補を作る
元の実例の着眼点を試すための独自のWebサンプルです。
名前と会社名を入れ、候補を使ってから表示名を書き換えてください。
実装の手順
- 名前・会社名と、補完したい欄を分ける。サンプルでは、この二項目から表示名の候補を組み立てる。
- 候補は自動で確定せず、内容と「使う」ボタンを隣に置く。推定処理をAPIに置き換える場合も、確定操作は利用者に残す。
- 採用した値は通常の入力欄に入れ、その後も自由に直せるようにする。
動作確認
二項目が揃うと候補が出る。利用者が「使う」を押すまで編集中の表示名が変わらない。
サンプルコードを見る
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>入力済みの情報から候補を作る</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 16px;
background: #23232a;
color: #f4f0ea;
font: 16px/1.6 system-ui, sans-serif;
}
label, output, strong, audio, progress { display: block; margin: 6px 0; }
input, select, button { max-width: 100%; font: inherit; }
input, select { padding: 4px 7px; }
button {
margin: 4px 0;
padding: 5px 12px;
cursor: pointer;
border: 1px solid #b5a399;
border-radius: 6px;
}
:focus-visible { outline: 3px solid #ffad73; outline-offset: 2px; }
audio { width: 100%; }
[hidden] { display: none !important; }
</style>
</head>
<body>
<label>名前 <input id="name" autocomplete="name"></label>
<label>会社名 <input id="org" autocomplete="organization"></label>
<button id="use" disabled>候補を使う</button>
<label>表示名 <input id="display"></label>
<script>
const nameField = document.querySelector('#name');
const org = document.querySelector('#org');
const use = document.querySelector('#use');
function suggestion() {
return nameField.value.trim() + ' / ' + org.value.trim();
}
function refresh() {
use.disabled = !nameField.value.trim() || !org.value.trim();
use.textContent = use.disabled ? '候補を使う' : suggestion() + ' を使う';
}
nameField.oninput = org.oninput = refresh;
use.onclick = () => { document.querySelector('#display').value = suggestion(); };
</script>
</body>
</html>index.html に保存し、ブラウザで開くと試せます。