Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Jquery Autocomplete - using the value instead of the label

Posted on 2013-01-04
11
446 Views
Last Modified: 2013-01-12
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.ClientID%>").autocomplete({
                       source: function (request, response) {
                           $.ajax({
                               url: '<%=ResolveUrl("~/Service.asmx/GetOccupationDescription") %>',
                               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.responseText);
                               },
                               failure: function (response) {
                                   alert(response.responseText);
                               }
                           });
                       },

                       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:TextBox>

I have a webservice which retrieves the data from the database called:
/Service.asmx/GetOccupationDescription

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)
0
Comment
Question by:TCBob
  • 7
  • 4
11 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 38744352
update your select part :

 select: function (event, ui) {
                           // Stop the Search input reloading the page 
			$("#<%= txtQtOccupation.ClientID %>").val(ui.item.value);
			return false;

                       },

Open in new window

0
 

Author Comment

by:TCBob
ID: 38744414
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
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38744419
I  still get the value displaying in the textbox
your 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;

                       },
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:TCBob
ID: 38744464
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 ?
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38744479
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;
0
 

Author Comment

by:TCBob
ID: 38744525
just implemented.

The hidden value doesn't get picked up, still picking up the label when submitting.

Thanks for trying though !
0
 
LVL 82

Expert Comment

by:leakim971
ID: 38744542
replace your hidden field by a textfield and let me know if you don't see the value after autocomplete
0
 

Author Comment

by:TCBob
ID: 38750099
ok will give that a try
0
 

Author Comment

by:TCBob
ID: 38753900
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
0
 

Accepted Solution

by:
TCBob earned 0 total points
ID: 38753908
The jquery advice was helpful but ultimately we resolved the issue by amending our vb code in conjunction with the jquery I had worked on
0
 

Author Closing Comment

by:TCBob
ID: 38769783
we had to change our own vb code to enable the hidden field to be picked up in the jquery
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question