【问题标题】:Pressing enter works on all other browsers except internet explorer按 Enter 可在除 Internet Explorer 之外的所有其他浏览器上使用
【发布时间】:2013-05-28 01:57:38
【问题描述】:

我的default.aspx 页面中有以下代码

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div class="default">
        <div class="search_panel">

        <select id="Select1"  name="search_type">
            <option>option1</option>
            <option>option2</option>
        </select><input name="search_text"  type="text" /><input name="search"  formaction="page.aspx" type="image" alt="submit"  src="images/search.jpg" />

            </div>
       </div>
</asp:Content>

当用户按下enter 时,搜索按钮适用于所有浏览器(firefox、chrome、safari),但不适用于 IE 8。为什么会这样?

【问题讨论】:

    标签: c# .net internet-explorer-8 cross-browser


    【解决方案1】:

    尝试在FormPanel 控件上设置DefaultButton 属性。

    例子:

    <form id="Form1"
            defaultbutton="search"
            runat="server">
    

    或者

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
      <asp:Panel runat="server" DefaultButton="search">
        <div class="default">
            <div class="search_panel">
              <select id="Select1"  name="search_type">
                <option>option1</option>
                <option>option2</option>
              </select>
              <input name="search_text" type="text" />
              <input name="search" id="search" formaction="page.aspx" type="image" alt="submit"  src="images/search.jpg" />
            </div>
         </div>
       </asp:Panel>
    </asp:Content>
    

    【讨论】:

    • 它显示以下错误“Panel1”的 DefaultButton 必须是 IButtonControl 类型的控件的 ID
    • @jonnydepp 如果可能,请将搜索按钮更改为 asp image button 控件。
    【解决方案2】:

    HTML 标准中没有规定 enter 应该提交表单。 IE 8 不提交表单并不是真正的错误;其他浏览器只擅长猜测 enter 应该做什么。

    为了安全起见,您应该显式地添加代码以让输入键提交表单。

    【讨论】:

    • keypress 事件添加JavaScript,如果您只想在焦点集中时提交回车键,则可以在input 上,或者如果您希望始终提交回车键,则在文档上。
    • 或者只做 Magnus 发布的内容;当您使用 DefaultButton 属性时,看起来 ASP.NET 会自动发出此 JavaScript。
    猜你喜欢
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2013-11-21
    • 2011-08-27
    • 2012-01-09
    • 2013-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多