Redirecting users when choosing from autocomplete?

I am trying to implement a jquery autocomplete plugin. Everything works for me, but something does not work properly.

Basically, I have an autocomplete list of employees. The list is created from the table in the sql database (employee_names and employee_ID) using the VB.NET handler (.ashx file). Data is formatted as: employee_name-employee_ID. So far, so good, and all employees are listed in auto-complete.

The problem is that I don’t know how to redirect the user to a specific page (e.g. employee_profile.aspx) when they selected the employee from autocomplete.

This is my redirect code, but it does not work as it should:

$('#fname2').result(function(event, data, formatted) { location.href = "employee_profile.aspx?id=" + data }); 

For instance; user selects. Will he redirect the user to employee_profile.aspx? Id = name of employee-employee identifier (for example: employee_profile.aspx? Id = John Doe-91210) instead of employee_profile.aspx? Id = 91210.

I know I can remove employee_ID with:

 formatResult: function(data, value) { return value.split("-")[1]; } }); 

But I don’t know how to pass this employee_ID to the redirect page.

Here is all my code:

 $().ready(function() { $("#fname2").autocomplete("AutocompleteData.ashx", { minChars: 3, selectFirst: false, formatItem: function(data, i, n, value) { return value.split("-")[0]; }, //Not used, just for splitting employee_ID //formatResult: function(data, value) { // return value.split("-")[1]; //} }); $('#fname2').result(function(event, data, formatted) { location.href = "employee_profile.aspx?id=" + data }); }); 

I know that I am very close, and it should be something really simple, but can someone help me?

EDIT

This solved it for me: formatted.split instead of data.split. The code:

 $('#fname3').result(function(event, data, formatted) { var employeeId = formatted.split("-")[1]; location.href = "employee_profile.aspx?id=" + employeeId }); 
+3
source share
2 answers

You say that it is successfully redirected, not going to ...

employee_profile.aspx? ID = 91210

He is going to ...

employee_profile.aspx? id = John Doe-91210 ??

If so ... then you can just do the interlacing inside your result function ...

 $('#fname2').result(function(event, data, formatted) { var employeeId = data.split("-")[1]; location.href = "employee_profile.aspx?id=" + employeeId }); 
+5
source

I think location.href should be window.location

0
source

Source: https://habr.com/ru/post/1394563/


All Articles