views:

254

answers:

2

Hi,

i'm trying to implement the jquery autocomplete plugin. I've got it up and running, but something is not working properly.

Basically i have a autocomplete-list of employees. The list is generated from a table in a sql-database (employee_names and employee_ID), using a VB.NET handler (.ashx file). The data is formatted as: employee_name-employee_ID. So far so good and all employees are listed in autocomplete.

The problem is that i don't know how to redirect a user to a certain page (for example employee_profile.aspx) when they've selected an employee from autocomplete.

This is my redirect-code, but it ain't working like it should:

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

For example; a user selects It will redirect a user to employee_profile.aspx?id=name of employee-id of employee (for example: employee_profile.aspx?id=John Doe-91210) instead of employee_profile.aspx?id=91210.

I know i can strip the employee_ID with:

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

But i do not know how to pass that employee_ID to the redirect-page..

Here my whole 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 i'm very close and it should be something really simple, but can anyone help me out?

EDIT

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

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

Are you saying that it is successfully redirecting but rather than going to...

employee_profile.aspx?id=91210

It is going to...

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

If that is the case... then you can simply perform your striping inside your result function...

$('#fname2').result(function(event, data, formatted) {
        var employeeId = data.split("-")[1];
        location.href = "employee_profile.aspx?id=" + employeeId
 });
Flash84x
Yep, they ARE redirected, but indeed to employee_profile.aspx?id=John Doe-91210.Your suggestion makes definitely sense, but it's not working: they wont be redirected to employee_profile.aspx.Code:$().ready(function() { $("#fname").autocomplete("AutocompleteData2.ashx", { minChars: 3, selectFirst: false }); $('#fname').result(function(event, data, formatted) { var employeeId = data.split("-")[1]; window.location.href = "employee_profile.aspx?id=" + employeeId });});But we're getting closer..
juno-2
So where is it redirecting to now?
Flash84x
Solved it! i had to use formatted.split instead of data.split. Thanks!
juno-2
A: 

I think location.href should be window.location

Coronatus
The window object is the global scope, so location refers to window.location unless there is local variable named location.
jholster