Jquery autocomplete on select event
Solution 1
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
Hope this helps
Solution 2
Well, if you want to store in session using asp.net mvc3 then do the following
$(".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 );
}
Related videos on Youtube
smart boy
Updated on July 13, 2020Comments
-
smart boy almost 4 years
I am using jQuery autocomplete and its working fine, now I want to store a variable in session from jQuery when following condition occurs.
When someone types any word jQuery shows suggestion dropdown, if someone select an item from that suggestion dropdown.
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:
$(".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.
-
Anant Dabhi over 11 yearscheck your error console what it says...?
-
smart boy over 11 yearsnothing showing in console @Anant
-
FIFA oneterahertz about 8 years@smartboy What are the jquery libraries you used??
-
FIFA oneterahertz about 8 yearsI posted some question regarding autocomplete.Can you check this and say what is wrong with that.stackoverflow.com/questions/36860915/…
-
-
smart boy over 11 yearsSelect event is not working as i told you in my question and i tried it again but got no success
-
Si Robinson over 11 yearsDon't think you need the JsonRequestBehavior.AllowGet setting as this is filtered as HTTPPOST
-
Snake Eyes over 11 yearsIs optional (JsonRequestBehavior.AllowGet)