jQuery 自动完成“附带损害”在其他领域

发布于 2024-08-22 21:26:38 字数 2109 浏览 5 评论 0原文

我有一个 jquery 自动完成字段工作正常,后面跟着一个日期输入字段。每当用户从自动完成列表中选择一个项目时,它都会正确选择该项目并触发 result(),但它也会在我的日期字段中创建“附带损害”,将所选自动完成项目的 ID 插入其中。这是其中一种情况,但我在其他情况下也注意到了这一点,有时如果其他输入字段位于自动完成字段之前或之后,并不重要。另外,如果有 3 个(任意数量)其他字段,则所有这三个字段都将插入自动完成项目 ID。

这是不受欢迎的行为,我需要摆脱它。 有人吗?

这是代码:

        $("#Clients").focus().autocomplete('<%=Url.Action("GetClients", "Client") %>', {
            dataType: 'json',
            parse: function(data) {
                var rows = new Array();
                for (var i = 0; i < data.length; i++) {
                    rows[i] = { data: data[i], value: data[i].ClientName, result: data[i].ClientName };
                }
                return rows;
            },
            formatItem: function(row, i, n) {
                return row.ClientName;
            },
            width: 300,
            minChars: 0,
            max: 0,
            delay: 50,
            cacheLength: 10,
            selectFirst: true,
            selectOnly: true,
            mustMatch: true
        });

        $("#Clients").result(function(event, data, formatted) {
            if (data) {
                $(this).parent().next().find("input").val(data["client_id"]);
                if (data["ClientName"] && data["client_address1"] && data["client_postcode"] && data["client_postname"]) {
                    $("#ClientDetails").html(
                    "<li class=\"clientNumber\">Client Id: " + data["client_ClientNumber"] + "</li>" +
                    "<li>" + data["ClientName"] + "</li>" +
                    "<li>" + data["client_address1"] + "</li>" +
                    "<li>" + data["client_postcode"] + data["client_postname"] + "</li>"
                    );
                }
                else {
                    $("#ClientDetails").html(
                    "<li class=\"clientNumber\">Client Id: " + data["client_ClientNumber"] + "</li>" +
                    "<li>" + data["ClientName"] + "</li>");
                }
            }
        });

I have a jquery autocomplete field working fine followed by a date input field. Whenever a user selects an item from autocomplete list it correctly selects the item and fires result() but it also creates "collateral damage" in my date field, inserting ID of the selected autocomplete item into it. This is one case of it but I also noticed it in other occasions, sometimes if other input fields are before autocomplete field or behind, doesn't really matter. Also If there are 3 (any number) other fields all three would be inserted the autocomplete item ID.

It is undesired behaviour and I need to get rid of it.
Anyone?

Here's the code:

        $("#Clients").focus().autocomplete('<%=Url.Action("GetClients", "Client") %>', {
            dataType: 'json',
            parse: function(data) {
                var rows = new Array();
                for (var i = 0; i < data.length; i++) {
                    rows[i] = { data: data[i], value: data[i].ClientName, result: data[i].ClientName };
                }
                return rows;
            },
            formatItem: function(row, i, n) {
                return row.ClientName;
            },
            width: 300,
            minChars: 0,
            max: 0,
            delay: 50,
            cacheLength: 10,
            selectFirst: true,
            selectOnly: true,
            mustMatch: true
        });

        $("#Clients").result(function(event, data, formatted) {
            if (data) {
                $(this).parent().next().find("input").val(data["client_id"]);
                if (data["ClientName"] && data["client_address1"] && data["client_postcode"] && data["client_postname"]) {
                    $("#ClientDetails").html(
                    "<li class=\"clientNumber\">Client Id: " + data["client_ClientNumber"] + "</li>" +
                    "<li>" + data["ClientName"] + "</li>" +
                    "<li>" + data["client_address1"] + "</li>" +
                    "<li>" + data["client_postcode"] + data["client_postname"] + "</li>"
                    );
                }
                else {
                    $("#ClientDetails").html(
                    "<li class=\"clientNumber\">Client Id: " + data["client_ClientNumber"] + "</li>" +
                    "<li>" + data["ClientName"] + "</li>");
                }
            }
        });

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

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

发布评论

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

评论(1

故事还在继续 2024-08-29 21:26:38

对我来说,这条线

$(this).parent().next().find("input").val(data["client_id"]);

看起来有点可疑。

  • 你正在得到的父母
    ID 为“Clients”的输入控件
  • 然后移至下一项
  • 查找此项中的所有 输入控件 将
  • 找到的所有 输入控件的值设置为 client_id

For me this line

$(this).parent().next().find("input").val(data["client_id"]);

looks a bit dubious.

  • You are getting the parent of the
    input control with the ID "Clients"
  • Then you move to the next item
  • Find all input controls within this item
  • Set the value of all found input controls to the client_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文