views:

66

answers:

2

I am trying to get an autocomplete working and making an Ajax form submission on change, and so far the first is working but not the second. I am using jQuery UI Autocomplete with Combobox, http://jqueryui.com/demos/autocomplete/#combobox. From my Django template, I define a handler, presently beginning with an alert that seems never to have been triggered:

function update_autocomplete(event, ui)
    {
    alert("Reached update_autocomplete");
    var split_value = ui.item.value.split(".");
    var field = split_value[0];
    var id = split_value[1];
    $.ajax({
        data:
            {
            id: "Entity_" + field + "_" + {{ entity.id }},
            value: id,
            },
        url: "/ajax/save",
        });
    }

And later on:

$(function()
    {
    $("#department").combobox();
    $("#reports_to").combobox();
    $("#department").bind("autocompletechange",
        update_autocomplete);
    $("#department").bind("autocompleteselect",
        update_autocomplete);
    $("#reports_to").bind("autocompletechange",
        update_autocomplete);
    $("#reports_to").bind("autocompleteselect",
        update_autocomplete);
    });

In the webpage rendered, the UI seems show the desired behavior apart from triggering the save-related event. Neither Chrome nor Firefox report errors. And the alert provisionally put in seems not to have triggered even once.

Am I approaching this problem in the right way? If I have the wrong approach, what is the correct approach to make a jQuery UI autocomplete that will save on an entry being chosen?

Thanks,

--edit--

I've replaced the second snippet with:

$(function()
    {
    $(".autocomplete").combobox();
    $(".autocomplete").autocomplete({select: update_autocomplete});
    $(".autocomplete").bind({"autocompleteselect": update_autocomplete});
    $(".autocomplete").bind({"autocompletechange": update_autocomplete});
    });

The behavior seems to be unchanged.

A: 

This works for me:

I set the event handler in the original autocomplete method call. I'm assuming that #department is the autocompleted input.

$("#department").autocomplete({
    source: my_source,
    select: function(event, ui) {
        update_autocomplete(event, ui);
    }
});

Hope this helps.

rdickert
Did you mean "update_autocomplete(event, ui)" or "update_autocomplete" alone? Thanks in advance (and retrospect),
JonathanHayward
Sorry, I made a mistake in the original post. I have revised my answer above. If calling "update_autocomplete" alone is working, you're good to go. In my parallel but not identical situation, it didn't work, but the above code should. I enclosed the call to update_autocomplete in an anonymous function.In most of my code, I can integrate the contents of "update_autocomplete" into the anonymous function itself, but if other code needs to be able call it, then the solution code should get it done.
rdickert
A: 

might just be your sample you provided but looks like your JSON is illegal.

>   $.ajax({
>         data:
>             {
>             id: "Entity_" + field + "_" + {{ entity.id }},
>             value: id,
>             },
>         url: "/ajax/save",
>         });

remove the extra comas

  $.ajax({
        data:
            {
            id: "Entity_" + field + "_" + {{ entity.id }},
            value: id
            },
        url: "/ajax/save"
        });
Chris
I believe that the final comma is optional, but permitted. The JavaScript interpreter is not appearing to give a syntax error.
JonathanHayward