2011-01-12 89 views
0

我遇到了一些JavaScript問題。JQuery問題/錯誤

請找到下面的代碼,我試圖做一個AJAX調用。當我手動輸入名稱(註釋代碼)時,它可以正常工作,但是當我使用AJAX調用時,它不會將正確的數據添加到下拉列表中。

$(function() { 
    /* var names = [ 
     { value: 1, label: "Marcus Ekwall" }, 
    { value: 1.1, label: "Marcus 1" }, 
     { value: 2, label: "John Resig" }, 
     { value: 3, label: "Eric Hynds" }, 
     { value: 4, label: "Paul Irish" }, 
     { value: 5, label: "Alex Sexton" } 
    ];*/ 

    var names = ""; 
    var container = $("div.ui-tagging"), 
    highlight = container.children("div.highlight"), 
     textArea = container.find("textarea"), 
    meta = container.children("input.meta"), 
     activeSearch = false, 
     searchTerm = "", 
     beginFrom = 0; 

    textArea.keypress(function(e){ 
     // activate on @ 
     if (e.which == 64 && !activeSearch) { 
      activeSearch = true; 
      beginFrom = e.target.selectionStart+1; 
     } 

     // deactivate on space 
     if (e.which == 32 && activeSearch) { 
      activeSearch = false; 
     } 
    }).bind("keyup change", function(e){ 
     var cur = highlight.find("span"), 
      val = textArea.val(); 
     cur.each(function(i){ 
      var s = $(this); 
      val = val.replace(s.text(), $("<div>").append(s).html()); 
     }); 
     highlight.html(val); 
    }).autocomplete({ 
     minLength: 0, 
     delay: 0, 
     open: function(event, ui) { 

      //console.log(ui); 
     }, 

     source: function(request, response) { 
      if (activeSearch) { 
       searchTerm = request.term.substring(beginFrom); 
       if (request.term.substring(beginFrom-1, beginFrom) != "@") { 
        activeSearch = false; 
        beginFrom = 0; 
        searchTerm = ""; 
       } 

       if (searchTerm != "") { 
        var re = new RegExp("^"+escape(searchTerm)+".+", "i"); 
        var matches = []; 
        $.ajax({ 
         url: '/search.asp?SearchTerm=' + searchTerm, 
         success: function(data) { 
          var names = data; 
          alert(names); 
         } 

        }); 

        $.each(names, function(){ 
         if (this.label.match(re)) { 
          matches.push(this); 
         } 
        }); 
        response(matches); 
       } 
      } 
     }, 

     focus: function() { 
      // prevent value inserted on focus 
      return false; 
     }, 

     select: function(event, ui) { 
      activeSearch = false; 
      //console.log("@"+searchTerm, ui.item.label); 

      this.value = this.value.replace("@"+searchTerm, ui.item.label)+' '; 
      highlight.html(highlight.html().replace("@"+searchTerm, '<span class="ui-corner-all">'+ui.item.label+'</span>')+' '); 

      meta.val((meta.val()+" @["+ui.item.value+":]").trim()); 
      return false; 
     } 
    }); 
}); 

回答

1

您需要考慮到Ajax的異步特性。要麼您需要通過在呼叫中設置async: false來使您的ajax呼叫同步,或者您需要使用姓名將代碼移動到success回叫的主體中。所以:

$.each(names, function(){ 
    if (this.label.match(re)) { 
    matches.push(this); 
    } 
}); 
response(matches); 

應該是成功的。否則,正如你所看到的,當你點擊它時,名字可能是空的。

0

正如justkt所說,如果您在得到來自AJAX請求的響應後調用所有代碼,仍然可以使用異步方式。這裏是一個清理代碼:

$(function() { 
    var names = "", 
     container = $("div.ui-tagging"), 
     highlight = container.children("div.highlight"), 
     textArea = container.find("textarea"), 
     meta = container.children("input.meta"), 
     activeSearch = false, 
     searchTerm = "", 
     beginFrom = 0, 
     bindTextAreaEvents = function (textArea, data) { 
      names = data; 

      textArea.keypress(function (e) { 
       // activate on @ 
       if (e.which === 64 && !activeSearch) { 
        activeSearch = true; 
        beginFrom = e.target.selectionStart + 1; 
       } 
       // deactivate on space 
       if (e.which === 32 && activeSearch) { 
        activeSearch = false; 
       } 
      }).bind("keyup change", function (e) { 
       var cur = highlight.find("span"), 
        val = textArea.val(); 
       cur.each(function (i) { 
        var s = $(this); 
        val = val.replace(s.text(), $("<div>").append(s).html()); 
       }); 
       highlight.html(val); 
      }).autocomplete({ 
       minLength: 0, 
       delay: 0, 
       open: function (event, ui) { 
        //console.log(ui); 
       }, 
       source: function (request, response) { 
        if (activeSearch) { 
         searchTerm = request.term.substring(beginFrom); 
         if (request.term.substring(beginFrom - 1, beginFrom) !== "@") { 
          activeSearch = false; 
          beginFrom = 0; 
          searchTerm = ""; 
         } 

         if (searchTerm !== "") { 
          var re = new RegExp("^" + escape(searchTerm) + ".+", "i"), 
           matches = []; 

          $.each(names, function() { 
           if (this.label.match(re)) { 
            matches.push(this); 
           } 
          }); 
          response(matches); 
         } 
        } 
       }, 
       focus: function() { 
        // prevent value inserted on focus 
        return false; 
       }, 
       select: function (event, ui) { 
        activeSearch = false; 
        //console.log("@"+searchTerm, ui.item.label); 
        this.value = this.value.replace("@" + searchTerm, ui.item.label) + ' '; 
        highlight.html(highlight.html().replace("@" + searchTerm, '<span class="ui-corner-all">' + ui.item.label + '</span>') + ' '); 

        meta.val((meta.val() + " @[" + ui.item.value + ":]").trim()); 
        return false; 
       } 
      }); 
     }; 

    $.ajax({ 
     url: '/search.asp?SearchTerm=' + searchTerm, 
     success: function (data) { 
      bindTextAreaEvents(textArea, data); 
     } 
    }); 
}); 
+0

謝謝 - 你的代碼我得到「不能調用方法匹配」 – Tech 2011-01-12 14:13:49