【问题标题】:ASP.NET TextBox With JQuery AutoComplete带有 JQuery 自动完成功能的 ASP.NET 文本框
【发布时间】:2011-06-06 13:03:49
【问题描述】:

我正在使用带有 asp.net 文本框的 JQuery UI 自动完成功能。 自动完成工作正常。 但是如何将返回数据的选定 ID 分配给 hiddenField? 我的服务器端函数返回的对象列表包含(这是一个示例):

 public List<Employee> GetEmployeeList()
{
    List<Employee> empList = new List<Employee>();
    empList.Add(new Employee() { ID = 1, Email = "Mary@somemail.com" });
    empList.Add(new Employee() { ID = 2, Email = "John@somemail.com" });
    empList.Add(new Employee() { ID = 3, Email = "Amber@somemail.com" });
    empList.Add(new Employee() { ID = 4, Email = "Kathy@somemail.com" });
    empList.Add(new Employee() { ID = 5, Email = "Lena@somemail.com" });
    empList.Add(new Employee() { ID = 6, Email = "Susanne@somemail.com" });
    empList.Add(new Employee() { ID = 7, Email = "Johnjim@somemail.com" });
    empList.Add(new Employee() { ID = 8, Email = "Jonay@somemail.com" });
    empList.Add(new Employee() { ID = 9, Email = "Robert@somemail.com" });
    empList.Add(new Employee() { ID = 10, Email = "Krishna@somemail.com" });

    return empList;
}

这是 ASPX 代码:

<form id="form1" runat="server">
<div class="demo">
    <div class="ui-widget">
        <label for="tbAuto">
            Enter Email:
        </label>
        <asp:TextBox ID="tbAuto" class="tb" runat="server">
        </asp:TextBox>
    </div>
</div>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<asp:Label runat="server" ID="lbl" Text=""></asp:Label>
<asp:HiddenField runat="server" ID="hidid" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>

这是我的 jquery 代码:

<script type="text/javascript">

    $(function () {


        $(".tb").autocomplete({

       select: function( event, ui ) {
       // now assign the id of the selected element into your hidden field
       $("#<%= hidid.ClientID %>").val( ui.item.ID ); 
         },

            source: function (request, response) {
                $.ajax({
                    url: "Default.aspx/FetchEmailList",
                    data: "{ 'mail': '" + request.term + "' }",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) { return data; },
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            return {
                                value: item.Email
                            }

                        }
                        )
                        )
                    }

                    ,
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                        alert(errorThrown);
                    }
                });
            },
            minLength: 1
        });
    });
</script>

这是我的 WEB 方法端代码:

<WebMethod()> _
Public Shared Function FetchEmailList(ByVal mail As String) As List(Of Employee)
    Dim emp = New Employee()
    Dim fetchEmail = emp.GetEmployeeList()
    Return fetchEmail
End Function

【问题讨论】:

  • 当然是老兄,祝你好运:-)
  • 你对需要 ASP.NET 隐藏字段的隐藏字段值做了什么?
  • @jwiscarson:我必须保留记录 ID,例如 Textbox 包含 Sellers 。我必须保留它的 id 才能对其执行操作(搜索、保存、...)
  • 这些操作是否只在服务器端执行?如果您使用 Web 方法来执行获取操作,那么您也可以将它们用于这些其他操作,从而避免尝试在 JavaScript 中使用 ASP.NET 控件的 ID。
  • 嗯,你能给我一个示例代码吗?

标签: .net asp.net jquery jquery-ui autocomplete


【解决方案1】:

您可以订阅success 事件并像这样从ui.item.id 获取ID

select: function( event, ui ) {
    // now assign the id of the selected element into your hidden field
    $("#<%= hidid.ClientID %>").val( ui.item.id ); 
}

我以一种有点骇人听闻的方式掌握了hidid 字段(我不记得首选的方法是什么),所以这是一个需要改进的领域,但这应该指出你正确方向。

【讨论】:

  • 对不起,英语不是我的母语……hackish 是什么意思? (我认为这意味着技巧), ui 是一个对象吗?谢谢。
  • @shaahin:在这种情况下,“hackish”(顺便说一句,这不是一个词,至少不是一个正式的词)意味着它可以工作,但可能并不完全正确。如果您使用的是 ASP.NET 4,则可以指定客户端 ID(您只需要确保它是唯一的),因此您无需使用 &lt;%= something.ClientID %&gt; 技巧。
  • 我写了这段代码 : select: function (event, ui) { // 现在将所选元素的 id 分配到隐藏字段中 //$("# ").val(ui.item.ID);警报(ui.item.ID); },但警报的文本是未定义的。有什么问题?
  • 我写了警报(ui.item.ID);而是 $("#").val(ui.item.ID );查看价值,它是未定义的,ID 是未知属性我把你的代码放在正确的地方了吗?
  • @shaahin:代码看起来在正确的位置。我使用来自 jQuery UI 网站的代码作为参考。也许您可以尝试先输入UI,然后查看该对象具有哪些属性,然后尝试找到您需要的属性。您可以在firefox中使用firebug在方法中放置断点并查看值。
猜你喜欢
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多