TCBob
asked on
Jquery Autocomplete - using the value instead of the label
The default action of autocomplete will grab the value and place into the input box. I need to use the value as we do a reverse lookup to check the correct value is being parsed. However to the user I want to display the label (text) here is my jquery code:
<script type="text/javascript">
$(document).ready(function () {
$("#<%=txtQtOccupation.Cli entID%>"). autocomple te({
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Service. asmx/GetOc cupationDe scription" ) %>',
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[1],
value: item.split('-')[0]
}
}))
},
error: function (response) {
alert(response.responseTex t);
},
failure: function (response) {
alert(response.responseTex t);
}
});
},
select: function (event, ui) {
// Stop the Search input reloading the page
event.preventDefault();
},
minLength: 3
});
});
</script>
<asp:TextBox id="txtQtOccupation" value="" runat="server" SkinID="Large" MaxLength="20"></asp:TextB ox>
I have a webservice which retrieves the data from the database called:
/Service.asmx/GetOccupatio nDescripti on
This all works correct, but the label data is picked up instead of the value when stepping through the code. I have tried using a hidden field but can't get this to work either.
I am using visual studio 2010 (4.5 framework)
<script type="text/javascript">
$(document).ready(function
$("#<%=txtQtOccupation.Cli
source: function (request, response) {
$.ajax({
url: '<%=ResolveUrl("~/Service.
data: "{ 'prefix': '" + request.term + "'}",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data.d, function (item) {
return {
label: item.split('-')[1],
value: item.split('-')[0]
}
}))
},
error: function (response) {
alert(response.responseTex
},
failure: function (response) {
alert(response.responseTex
}
});
},
select: function (event, ui) {
// Stop the Search input reloading the page
event.preventDefault();
},
minLength: 3
});
});
</script>
<asp:TextBox id="txtQtOccupation" value="" runat="server" SkinID="Large" MaxLength="20"></asp:TextB
I have a webservice which retrieves the data from the database called:
/Service.asmx/GetOccupatio
This all works correct, but the label data is picked up instead of the value when stepping through the code. I have tried using a hidden field but can't get this to work either.
I am using visual studio 2010 (4.5 framework)
ASKER
Thanks for the swift reply, having implemented your suggestion I still get the value displaying in the textbox when I require the label to display to the user and the value in the code
I still get the value displaying in the textboxyour question title say : using the value instead of the label
what about :
select: function (event, ui) {
// Stop the Search input reloading the page
$("#<%= txtQtOccupation.ClientID %>").val(ui.item.label);
return false;
},
ASKER
Again thanks for the quick response, using this code:
select: function (event, ui) {
// Stop the Search input reloading the page
$("#<%= txtQtOccupation.ClientID %>").val(ui.item.label);
return false;
works on the page functionality wise, but I now submit the label when I should submit the
value. Any further idea's ?
select: function (event, ui) {
// Stop the Search input reloading the page
$("#<%= txtQtOccupation.ClientID %>").val(ui.item.label);
return false;
works on the page functionality wise, but I now submit the label when I should submit the
value. Any further idea's ?
select: function (event, ui) {
// Stop the Search input reloading the page
$("#<%= hdnQtOccupation.ClientID %>").val(ui.item.value);
$("#<%= txtQtOccupation.ClientID %>").val(ui.item.label);
return false;
// Stop the Search input reloading the page
$("#<%= hdnQtOccupation.ClientID %>").val(ui.item.value);
$("#<%= txtQtOccupation.ClientID %>").val(ui.item.label);
return false;
ASKER
just implemented.
The hidden value doesn't get picked up, still picking up the label when submitting.
Thanks for trying though !
The hidden value doesn't get picked up, still picking up the label when submitting.
Thanks for trying though !
replace your hidden field by a textfield and let me know if you don't see the value after autocomplete
ASKER
ok will give that a try
ASKER
thanks for your attempts on this. We have used a hidden field but needed to also do some work on the vb side of things on the site
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
we had to change our own vb code to enable the hidden field to be picked up in the jquery
Open in new window