简体   繁体   English

jQuery自动完成MVC3

[英]jquery autocomplete MVC3

I've got the functionality I need almost complete and am looking for a little help. 我已经拥有几乎需要的功能,并且正在寻找一些帮助。 I have a model with a VendorID property. 我有一个带有VendorID属性的模型。 I have a strongly typed view using this model. 我使用此模型有一个强类型的视图。 I allow the user to select a vendor from an input text box that uses auto complete. 我允许用户从使用自动完成的输入文本框中选择一个供应商。

I bind the model's VendorID property to a hidden field on the view with this razor syntax. 我使用这种剃刀语法将模型的VendorID属性绑定到视图上的隐藏字段。

@Html.HiddenFor(m => m.VendorID)

When the user types in a few characters in to the textbox and selects an item, the jquery function I've got sets the value of the hidden field. 当用户在文本框中输入几个字符并选择一个项目时,我得到的jquery函数将设置隐藏字段的值。 Up to this point, everything works great. 到目前为止,一切正常。 The problem I have is if the user clears the textbox completely or enters invalid text, whereas there isn't a valid value selected, and my hidden field can't be updated. 我遇到的问题是,如果用户完全清除了文本框或输入了无效文本,而没有选择有效值,并且我的隐藏字段无法更新。

Here is my jquery I have so far. 这是我到目前为止的jQuery。

$(document).ready(function () {
    $('#vendorautocomplete').val($('#VendorName').val());  //prepopulate our vendorautocomplete textbox from our model
});

$(function () {

    $('#vendorautocomplete').autocomplete({

        source: function (request, response) {
            $.ajax({
                url: "/test/vendors", type: "POST", dataType: "json", //Url to our action supplying a list of vendors
                data: { searchString: request.term, maxRecords: 10 }, 
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.VendorName, value: item.VendorName, id: item.VendorID };
                    }));
                }
            });
        },
        select: function (event, ui) {
            $('#VendorID').val(ui.item ? ui.item.id : 0); //Set our VendorID hidden for model binding
        }

    });
});

I was hoping I could do something in the Autocomplete function similar to select: above only for blur:, but that didn't work. 我希望可以在自动完成功能中执行类似于select:的操作,仅用于模糊:,但这不起作用。 I suppose I could use the blur event to look up the ID of the vendor based on the value in the textbox, but it seems at some point I should be able to detect the invalid vendor name and set the hidden field to 0, I just don't know where. 我想我可以使用blur事件根据文本框中的值查找供应商的ID,但是似乎在某个时候我应该能够检测到无效的供应商名称并将隐藏字段设置为0,我只是不知道在哪里

I managed to accomplish what I wanted to do by adding the following to my scripts. 通过将以下内容添加到脚本中,我设法完成了我想做的事情。

For bad search data (no values returned) 对于错误的搜索数据(不返回任何值)

success: function (data) {
                    var arr = $.map(data, function (item) { return { label: item.VendorName, value: item.VendorName, id: item.VendorID }; });
                    if (arr.length == 0) {
                        $('#VendorID').val(null);
                        $('#VendorName').val('');
                    }
                    response(arr);
                }

I check for a blank text box on the change event 我在更改事件中检查是否有空白文本框

  $(function () {
      $('#vendorautocomplete').change(function () {
          if ($('#vendorautocomplete').val() == null || $('#vendorautocomplete').val() === "") {
              $('#VendorID').val(null);
              $('#VendorName').val('');
          }
      });
  });

Not sure it's the best solutuion, but it works and I need to move onto other problems. 不确定这是否是最好的解决方案,但它是否有效,我需要谈谈其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM