jQuery 通过 JSON 自动完成来自 ASMX 的名称/值对

发布于 2024-10-01 01:23:03 字数 769 浏览 1 评论 0原文

我有两个文本框,我试图将其用于自动完成。两者的源数据均来自 ASP.NET ASMX Web 服务,以 JSON 格式返回。我正在返回一个列表,其中 NameValue 定义为:

public struct NameValue { public string Name;公共字符串值; }

我如何解析此数据,以便可以将所选下拉列表名称的值存储在隐藏字段中?

这是页面加载时到目前为止我的代码,并且 msg.d 包含 JSON 数据(我看到 msg.d[0].Namemsg. d[0].值)

$.ajax({
        type: "POST",
        url: '/Services/Team.asmx/GetClubTeams',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
          $('#<%= txtFromTeam2.ClientID %>, #<%= txtToTeam2.ClientID %>').autocomplete({
            source:msg.d
          });
        },
        error: function(xhr, msg) {
          alert(msg);
        }
      });

I have two textboxes which I am trying to use for autocomplete. The source data for both is coming from an ASP.NET ASMX web service, returned in JSON format. I'm getting back a List where NameValue is defined as:

public struct NameValue { public string Name; public string Value; }

How can I parse this data such that the selected dropdown Name's Value can be stored away in a hidden field?

Here is my code thus far when the page loads, and msg.d contains the JSON data (I see msg.d[0].Name and msg.d[0].Value)

$.ajax({
        type: "POST",
        url: '/Services/Team.asmx/GetClubTeams',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
          $('#<%= txtFromTeam2.ClientID %>, #<%= txtToTeam2.ClientID %>').autocomplete({
            source:msg.d
          });
        },
        error: function(xhr, msg) {
          alert(msg);
        }
      });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2024-10-08 01:23:03

回答了我自己的问题。诀窍是要知道名称/值应采用以下形式:

public struct TeamData { public string key;公共字符串值; 。

看来value(小写)对 jQuery 很重要

$.ajax() 调用如下...希望这对某人有帮助:

$.ajax({
       type: "POST",
       url: '/Services/Team.asmx/GetClubTeams',
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       data: "{}",
       success: function(data) {
         $('#txtFromTeam2').autocomplete({
           source: data.d,
           minLength: 3,
           focus: function(event, ui) {
             $('#txtFromTeam2').val(ui.item.value);
             return false;
           },
           select: function(event, ui) {
             $('#txtFromTeam2').val(ui.item.value);
             $('#<%= txtFromTeam2Id.ClientID %>').val(ui.item.key);
             populatePlayers(ui.item.key);
             return false;
           }
         });
         $('#txtToTeam2').autocomplete({
           source: data.d,
           minLength: 3,
           focus: function(event, ui) {
             $('#txtToTeam2').val(ui.item.value);
             return false;
           },
           select: function(event, ui) {
             $('#txtToTeam2').val(ui.item.value);
             $('#<%= txtToTeam2Id.ClientID %>').val(ui.item.key);
             return false;
           }
         });
       },
       error: function(xhr, msg) {
         alert(msg);
       }
     });

Answered my own question. The trick is to know that the Name/Value should be of the form:

public struct TeamData { public string key; public string value; }

It appears value (lowercase) matters to jQuery.

The $.ajax() call is as follows...hope this helps someone:

$.ajax({
       type: "POST",
       url: '/Services/Team.asmx/GetClubTeams',
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       data: "{}",
       success: function(data) {
         $('#txtFromTeam2').autocomplete({
           source: data.d,
           minLength: 3,
           focus: function(event, ui) {
             $('#txtFromTeam2').val(ui.item.value);
             return false;
           },
           select: function(event, ui) {
             $('#txtFromTeam2').val(ui.item.value);
             $('#<%= txtFromTeam2Id.ClientID %>').val(ui.item.key);
             populatePlayers(ui.item.key);
             return false;
           }
         });
         $('#txtToTeam2').autocomplete({
           source: data.d,
           minLength: 3,
           focus: function(event, ui) {
             $('#txtToTeam2').val(ui.item.value);
             return false;
           },
           select: function(event, ui) {
             $('#txtToTeam2').val(ui.item.value);
             $('#<%= txtToTeam2Id.ClientID %>').val(ui.item.key);
             return false;
           }
         });
       },
       error: function(xhr, msg) {
         alert(msg);
       }
     });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文