如果没有结果,如何禁用jquery ajax- auto complete?

时间:2013-12-11 06:38:21

标签: javascript jquery ajax

   jQuery("#" + msgSubject).autocomplete({
       source: function (request, response) {
           jQuery.ajax({
               url: inboxControl.systemParams.allTemplateData + '/' + request.term + inboxControl.systemParams.API_EXTENSION,
               dataType: "json",
               async: false,
               contentType: 'application/json; charset=utf-8',

               error: function (jq, status, message) {
                   //alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message); 
               },
               success: function (data) {

                   if (data.results) {
                       response(data.results.templates);
                   } else {

                       response("");
                   }

               }
           });
       },
       minLength: 1

   });

这是我的代码,我想要禁用ajax调用,如果没有输入文本的结果,在这种情况下我想搜索单词“India”,如果我输入“I”它将进行ajax调用和搜索对于带有“I”的单词,假设结果为null,但如果我再次输入“n”作为第二个字母,它将使用“In”进行ajax调用,如果没有响应,我想停止第二个ajax调用第一个请求

2 个答案:

答案 0 :(得分:1)

您可以添加一个标志,然后根据所述标志的值有条件地发出ajax请求:

var has_match = true;
jQuery("#" + msgSubject).autocomplete({
    source: function (request, response) {          
        if(has_match) {
            jQuery.ajax({
                url: inboxControl.systemParams.allTemplateData + '/' + request.term + inboxControl.systemParams.API_EXTENSION,
                dataType: "json",
                async: false,
                contentType: 'application/json; charset=utf-8',

                error: function (jq, status, message) {
                    //alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message); 
                },
                success: function (data) {

                    if (data.results) {
                        response(data.results.templates);
                    } else {
                        has_match = false;
                    }

                }
            });
        }
    },
    minLength: 1

});

答案 1 :(得分:0)

   this.msgSubjectClick = function () {
        jQuery("." + preparams.displaySettings.msgSubjectClass).click(function () {

            var msgSubject = jQuery(this).attr('id');   

            if(msgSubject=="patientSubject")
            {
                var subjectID=preParams.displaySettings.patientSubjectId;
            }else
            {
               var ret = msgSubject.split("_");
               var str1 = ret[0];
               var str2 = ret[1];
               var subjectID=preParams.displaySettings.subjectID+str2;
             }

            var has_match = true;
            inboxControl.has_match=has_match;
            jQuery("#" + msgSubject).autocomplete({
                source: function (request, response) {
                 if(inboxControl.has_match) {
                    jQuery.ajax({
                        url: inboxControl.systemParams.allTemplateData + '/' + request.term + inboxControl.systemParams.API_EXTENSION,
                        dataType: "json",
                        async: false,
                        contentType: 'application/json; charset=utf-8',
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader(preParams.API_KEY, connSysParams.apiSecretKey);
                        },
                        error: function (jq, status, message) {
                            //alert('A jQuery error has occurred. Status: ' + status + ' - Message: ' + message); 
                        },
                        success: function (data) {

                            if (data.results) {
                                response(data.results.templates);
                            } else {

                                inboxControl.has_match = false;                             
                                jQuery("#" +subjectID).val(""); 
                                response("");
                            }

                        }
                    });
                    }
                },
                minLength: 1,
                select: function( event, ui )   {   

                jQuery("#" +subjectID).val(ui.item.id); 
            },


            });


        }).keypress(function(event) {

         var keycode = (event.keyCode ? event.keyCode : event.which);

            if(keycode == '8') {

                    inboxControl.has_match = true;
             }

         });
    };

haii..i已经用jquery中的密钥代码解决了这个问题,如果我们按回空格它会重置has_match的值...谢谢......