Saturday, May 21, 2011

jquery Autocomplete with previous dropdown

The html is simple we have a  dropdown box, ddlist and a textbox called textbox.
Please double check the names, I changed them in notepad.

This solution also fixes the issue where the dropdown list item is cashing the selected value from page load.

The first function erases the value of the textbox when the dropdown list changes.


$("#ddlist").change(function () {
    alert($('#ddlist option:selected').text());
    $("#textbox").val('');
});
 
This function kicks off the autocomplete process when the user clicks in the box.
If the value of the ddl is not good, then throw up a hint. 
$("#textbox").live("focus"function () {
    var marketVal = $('#ddlist option:selected').text();
    if (marketVal != '') {
        var sourceUrl = 'mypage.ashx?action=dosomething&ddlistName=' + $('#ddlist option:selected').text();
        $("#textbox").autocomplete({
            source: sourceUrl,
            minLength: 2
            //select: function (event, ui) { $("#SubMarket").val(ui.item.id); }
        });
    } else {
        alert("Please choose a Market item first");
    }
});

No comments:

Post a Comment