Javascript 选择事件上的 Jquery 自动完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12722891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:06:59  来源:igfitidea点击:

Jquery autocomplete on select event

javascriptjqueryasp.net-mvcjquery-uijquery-ui-autocomplete

提问by smart boy

I am using jQuery autocomplete and its working fine, now I want to store a variable in session from jQuery when following condition occurs.

我正在使用 jQuery 自动完成功能并且它工作正常,现在我想在发生以下条件时从 jQuery 在会话中存储一个变量。

When someone types any word jQuery shows suggestion dropdown, if someone select an item from that suggestion dropdown.

当有人输入任何单词时,jQuery 会显示建议下拉列表,如果有人从该建议下拉列表中选择了一个项目。

I want to capture above point and store a variable in session.

我想捕获以上点并在会话中存储一个变量。

I searched Google, StackOverflow but find no relevant solution. My code for autocomplete is following:

我搜索了谷歌、StackOverflow,但没有找到相关的解决方案。我的自动完成代码如下:

  $(".autosearch-smart").autocomplete('Home/GetCompanyNames', {
    minChars: 1,
    width: 402,
    matchContains: "word",
    autoFill: true
});

and this is what I tried to do:

这就是我试图做的:

  $(".autosearch-smart").autocomplete('Home/GetCompanyNames', {
    minChars: 1,
    width: 402,
    matchContains: "word",
    autoFill: true,
    select: function (a, b) {
        alert("selected");
    }

});

EDIT : Select event handler is also not working

编辑:选择事件处理程序也不起作用

I am using asp.net MVC3 with C#. Please help me out and thanks in advance.

我在 C# 中使用 asp.net MVC3。请帮助我,并提前致谢。

回答by iBoonZ

So if I understand correctly you want to store the selected value in a variable sessions

因此,如果我理解正确,您想将所选值存储在可变会话中

you can get the value out of the selected item through following code:

您可以通过以下代码获取所选项目的值:

  $(".autosearch-smart").autocomplete('Home/GetCompanyNames', {
minChars: 1,
width: 402,
matchContains: "word",
autoFill: true,
select: function (event, ui) {
    var label = ui.item.label;
    var value = ui.item.value;
   //store in session
  document.valueSelectedForAutocomplete = value 
}
});

the value and label are json objects that came from the server

值和标签是来自服务器的 json 对象

Hope this helps

希望这可以帮助

回答by Snake Eyes

Well, if you want to store in session using asp.net mvc3 then do the following

好吧,如果您想使用 asp.net mvc3 存储在会话中,请执行以下操作

$(".autosearch-smart").autocomplete('Home/GetCompanyNames', {
    minChars: 1,
    width: 402,
    matchContains: "word",
    autoFill: true,
    select: function (event, ui) {   //must be cleared with function parameter
        //alert(ui.item.label);  //will show you the selected item

       $.ajax({
          type: 'POST',
          url: '/Controller/Action1',  //whatever any url
          data: {label: ui.item.label},
          success: function(message) { if(message.data == true) ... else ... },
          dataType: 'json'
       });

    }

});

and controller

和控制器

[HttpPost]
  public JsonResult Action1( string label ) {

     this.Session["AnyValue"] = label;

     return Json( new {
        data = true
     }, JsonRequestBehavior.AllowGet );
  }