开发者

Accessing relative elements in jquery ui autocomplete?

I am attempting to send additional parameters with jqueryUI's autocomplete using an abstract approach. A stripped down version of the html being used is:

<form id='employees'>
   <input class='autocomplete' name='id_number' />
</form>

What I need is to be able to find the id of the parent relative to any autocomplete input (as there can be multiple on one page). The code I have so far is:

    $(".autocomplete").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: "search.php",
                data: {
                    form_id: // need help!
                    string: request.term
                },
                success: function( data ) {
                    // do stuff
                }
            });
        }
    });

So what I require to send as the form_id would be:

$(this).parent().attr('id')

The problem is, however, that using using $(this) once开发者_如何学Python inside the source function in the $.ajax call loses reference to the original element, and I don't see any point prior to that I can create a variable referencing the original element.

How would one go about obtaining the id of the parent to be sent as a parameter?

Thanks for any help!


Why don't you do like the following code? It can give you an idea...

$(".autocomplete").each(function() {
    var ac = $(this);
    var parentId = $(this).parent().attr('id');       
    ac.autocomplete({
        minLength: 3,
        source: function(request, response) { 
            findSuggestions(request, response, 'artist', artist_cache, parentId);
        }
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜