WebSurfer's Home

トップ > Blog 1   |   ログイン
APMLフィルター

submit せずに IE の Auto Complete に登録

by WebSurfer 2011年7月23日 14:09

IE にはオートコンプリートという機能があって、TextBox に前回入力した文字列を入力中に補完してくれます(Firefox など他のブラウザにも同等の機能がありますが、今回は IE に限った話です)。

IE のオートコンプリート機能

TextBox に入力した文字列が IE のオートコンプリートに登録される条件は、その TextBox が form の中にあって、form が submit タイプのボタン(例: <input type="submit" ... )のクリックで submit されることです。

button タイプのボタン(例: <input type="button" ... )の onclick 属性にスクリプトを設定して、そのスクリプトで form を submit しても、オートコンプリートには登録されません。

詳しくは、マイクロソフトサポートオンラインのページ BUG: AutoComplete Does Not Work When You Use Script to Submit a Form を参照してください。

ただし、上記のページにも出ていますが、AutoCompleteSaveForm Method を利用すると、submit せずにオートコンプリートに登録することができます。

具体的なコード例は以下の通りです。(上記の AutoCompleteSaveForm Method のコードを ASP.NET の TextBox を使って書き換えただけです)

なお、AutoCompleteSaveForm メソッドは IE5 以降のバージョン専用だそうですので注意してください。もちろん、Firefox, Chrome などでは動きません。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
  <script type="text/javascript">
  //<![CDATA[
    function fnSaveForm() {
      window.external.AutoCompleteSaveForm(form1);
      document.getElementById("AutoCompleteTest").value = "";
      document.getElementById("AutoCompleteIgnore").value = "";
    }
  //]]>
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    This text is saved: 
    <asp:TextBox ID="AutoCompleteTest" 
      runat="server">
    </asp:TextBox>
    <br />
    This text is not saved:
    <asp:TextBox ID="AutoCompleteIgnore" 
      runat="server" 
      AutoCompleteType="Disabled">
    </asp:TextBox>
    <br />
    <input id="Button1" 
      type="button" 
      value="Save Value" 
      onclick="fnSaveForm()" />
  </div>
  </form>
</body>
</html>

Tags: ,

JavaScript

About this blog

2010年5月にこのブログを立ち上げました。主に ASP.NET Web アプリ関係の記事です。

Calendar

<<  2024年3月  >>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

View posts in large calendar