我遇到了一些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;
}
});
});
謝謝 - 你的代碼我得到「不能調用方法匹配」 – Tech 2011-01-12 14:13:49