How to make another ajax call upon selection of autocomplete text field value in ASP.NET MVC 4?

ぐ巨炮叔叔 提交于 2019-12-18 07:22:38

问题


I have an autocomplete text field, that uses JSON like so:

          $(function () {
              var src = '@Url.Action("GetParts", "Parts")'
              $("#autoCompleteBox").autocomplete({
                  source: function (request, response) {
                      $.ajax({
                          url: src,
                          async: true,
                          dataType: "json",
                          data: {
                              partNumber: $("#autoCompleteBox").val()
                          },
                          success: function (data) {
                              response(data[0]);
                          }
                      });
                  }
              });
          });

What I want to do is when the user selects the item from the suggested list is make another ajax call to get specific information about that item and populate other textboxes on the page.

What is the best approach for this?


回答1:


You can do that in the select event of the autocomplete.

$(function () {
    var src = '@Url.Action("GetParts", "Parts")'
    $("#autoCompleteBox").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: src,
                async: true,
                dataType: "json",
                data: {
                    partNumber: $("#autoCompleteBox").val()
                },
                success: function (data) {
                    response(data[0]);
                }
            });
        },
        select: function (event, ui) {
           var item= ui.item.label;
           //Now make the ajax call here
           $.post("SomeValidUrl", new { id : item } ,function(res){
               // do something with res
           });
        }
    });
});


来源:https://stackoverflow.com/questions/35753808/how-to-make-another-ajax-call-upon-selection-of-autocomplete-text-field-value-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!