0

autocomplete在 IE 中不会选择鼠标单击的jQuery选择。(textbox即在GridView,内EditItemTamplate)仍然使用在文本框中键入的而不是从 IE 的下拉列表中选择的值发回。它在 Google 和 Firefox 中运行良好。

此代码在我的内容页面中。当我在 中键入一个字母时texboxautocomplete给我一个选项列表。如果我使用“向上”或“向下”箭头并选择该选项,它会正确填充我的textbox。但是,如果我通过鼠标单击选择了一个选项,那么它不会textbox用我选择的选项填充我,它只会用我在textbox.

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="BlindKeyVerification.aspx.cs" Inherits="Test.Administration.BlindKeyVerification" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <link href="../Styles/jquery-ui.css" rel="stylesheet" />
    <link href="../Styles/jquery-ui.min.css" rel="stylesheet" />
    <script src="../Scripts/jquery.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-ui.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-ui.min.js" type="text/javascript"></script>  
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">       

    $(document).ready(function () {

        $("[id*=txtGridCode]").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '<%=ResolveUrl("/Administration/priceCodeService.asmx/getPriceCodeArray") %>',
                    data: "{ 'priceCode': '" + request.term + "'}",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",                        
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {

                                label: item,                                    
                                val: item.split('-')[0]
                            }
                        }))
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }

                });
            },

            minLength: 1
        });

        $("[id*=txtGridCode]").autocomplete({
            select: function (event, ui) {
                $this = $(this);
                setTimeout(function () {
                    $("[id*=txtGridCode]").val(ui.item.val);                       
                }, 1);
            }
        });

    });

    function InitializeRequest(sender, args) {
        $("*").css("cursor", "wait");
    }

    function EndRequest(sender, args) {
        $("*").css('cursor', 'auto');
    }

    function stopRKey(evt) {
        var evt = (evt) ? evt : ((event) ? event : null);
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        //if ((evt.keyCode == 13) && (node.type == "text")) { return false; } //Stop return Key from Posting the page
        if ((evt.keyCode == 8) && (node.type == "select-one")) { return false; } //dropdown box backspace stop backpage
    }
    document.onkeypress = stopRKey; //Firefox
    document.onkeydown = stopRKey; //i.e.
</script>

textbox里面的一些部分GridView

BlinkKeyVerfication.aspx

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField HeaderText="Code">
            <ItemTemplate>
                <asp:Label ID="lblCode" runat="server" ForeColor="Red" Font-Size="Smaller">
                </asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtGridCode" runat="server" OnTextChanged="txtGridCode_TextChanged" AutoPostBack="true"></asp:TextBox>
            </EditItemTemplate>
            <ItemStyle Font-Size="Smaller" />
        </asp:TemplateField>
    </Columns>
</asp:GridView>

priceCodeService.asmx.cs

public partial class WebForm1 : System.Web.UI.Page
{

    public List<string> getPriceCodeArray(string priceCode)
    {
        List<string> doc = new List<string>();
        string CSConn = "Server=CourtData;Initial Catalog= CourtSupport; Integrated Security = SSPI";
        using (SqlConnection conn = new SqlConnection(CSConn))
        {
            using (SqlCommand comm = new SqlCommand("SELECT  priceCode, priceText FROM tblpriceCodeExtract WHERE priceCode like @priceCode + '%'", conn))
            {
                SqlParameter parameter_code = new SqlParameter();
                parameter_code.ParameterName = "@priceCode";
                parameter_code.Value = priceCode;
                comm.Parameters.Add(parameter_code);
                conn.Open();
                SqlDataReader rdr = comm.ExecuteReader();
                while (rdr.Read())
                {
                    //string doccode = rdr["priceCode"].ToString();
                    //string codetext = rdr["priceText"].ToString();
                    //if (codetext.Length > 30)
                    //    codetext = codetext.Substring(0, 29) + "...";
                    //doc.Add(string.Format("{0}-{1}", doccode, codetext));
                    doc.Add(rdr["priceCode"].ToString());

                }
            }
        }
        return doc;
    }
}
4

1 回答 1

0

由于兼容性视图,我的代码无法正常工作,我们的网站处于兼容性视图中。

在我的 aspx 内容页头部分中,我有以下代码,但它没有任何用处,因为我的母版页代码阻止了它

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

然后我从内容页面中删除了该代码,并在我的 .cs 文件中添加了以下代码

 protected void Page_Load(object sender, EventArgs e)
        {
            if (Header.Controls[1].GetType() != typeof(System.Web.UI.HtmlControls.HtmlMeta))
            {
                System.Web.UI.HtmlControls.HtmlMeta meta = new System.Web.UI.HtmlControls.HtmlMeta();
                meta.HttpEquiv = "X-UA-Compatible";
                meta.Content = "IE=edge";
                this.Header.Controls.AddAt(1, meta);
            }

现在我的代码在所有浏览器中都可以正常工作。现在我的代码中不再需要任何选择事件。所以我删除了它。

于 2015-09-30T12:19:42.967 回答