2013-12-11 100 views
2
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調用,如果沒有輸入文本的結果,在這種情況下,我想搜索一個單詞「印度」,如果我鍵入「我」它會做一個ajax調用並用「I」搜索單詞,假設結果爲空,但如果我再次輸入「n」作爲第二個字母,它將使用「In」進行ajax調用,如果不存在,我想停止第二個ajax調用如何禁用jquery ajax-自動完成如果沒有結果?

回答

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 

}); 
+0

感謝您的答覆..謝謝你哦。 .its完美:) :) –

+0

很高興爲你工作:) –

+0

現在我在這發現了一個新問題,假設我輸入了一個單詞「INDIE」而不是「INDIA」,它將返回null,它不會調用ajax請求,但如果我從「INDIE」中刪除字母「E」,就像「INDI」它不會給出結果,Becaus「has_match」的值爲false,我該如何解決這個問題? :( –

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的值...謝謝...