开发者

Check if asp.net control exists with jQuery

When referring asp.net control inside a MasterPage in this way:

    $(function() { 
        $('#开发者_运维问答<%=txtMunicipio.ClientID%>').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: 'http://localhost/Autocomplete/WSAutocomplete.asmx/Poblacion',
                    data: 't=' + request.term,
                    type: 'POST',
                    //contentType: 'text/xml;charset=utf-8',
                    dataType: 'xml',
                    success: function(data) {
                        response(a = $.map(splitResponse(data), function(a, n) {
                            return formatCity(a, n);
                        }));
                    }
                });
            },
            ........

I am getting error "Object expected" because control does not exist in page. How can I check if control exists before associate autocomplete function?

Thanks in advance


First basic thing, make sure jquery and autocomplete js files are included.

If you have a master page the ID might get changed and even if you don´t, the safest way i find is if($(´[id$=txtMunicipio]´)!=undefined && $(´[id$=txtMunicipio]´).length > 1) { //do your thing } else { //handle the problem }

and your problem might just be that you need to wrap your code around a: $(document).ready(function(){ //your code goes here });

and in case of an update panel, if you wanna run a script on every load go for: function pageLoad(){ //your code here }


$("#mydiv").length > 0

http://aaronrussell.co.uk/legacy/check-if-an-element-exists-using-jquery/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜