Noobie通过Ajax autosuggest表单混淆了他的方式:
我已经完成了大部分工作,但需要稍微分解一下,所以我可以理解jQuery是如何引用的.编辑:这是我的工作代码,谢谢你们.关于ajaxdiv延迟的一些问题,没有闲逛,但我正在努力;)
function jQajax(ipfield,ajd,dbtable,company,select,element){
if (!ipfield || !ajd || !dbtable || !company || !select || !element){alert("Parameters not sent to ajax handler correctly; Input to act on, Ajax Div, Database table to check, Company (database name), Field to select, Element Id. "); return false;}
actobj="#"+ipfield;// set the active object for jQuery
ajdiv="#"+ajd; //set the ajax responding div
listindex=-1; //clear the notion of which item is selected
scriptgo=1; // slowdown for key javascript
leftpos = findPos($(actobj));
var width = $(actobj).width()-2;
$(ajdiv).css("left",leftpos);
$(ajdiv).css("width",width);
$(actobj).keyup(function(event){
//alert(event.keyCode);
//Key presses you need to know: 40=down 38=up 13=Enter 27=ESC 8=Bkspc 46=DEL
var keyword = $(actobj).val();
if(keyword.length)
{
if(event.keyCode != 40 && event.keyCode != 38 && event.keyCode != 13)
{
$.ajax({
type: "GET",
url: "includes/ajax_server.php",
cache: false,
data: "company="+company+"&data="+keyword+"&table="+dbtable+"&select="+select,
success: function(msg){
if(msg != 0){
$(ajdiv).fadeIn("slow").html(msg);
fader();
}else{
$(ajdiv).fadeOut("slow");
}
}
});
}
else
{
switch (event.keyCode)
{
case 40: // down pressed
{
fader();
step=1;
mvIndex(step);
}
break;
case 38: //up pressed
{
fader();
step=-1;
mvIndex(step);
}
break;
case 13:
{
$(actobj).val($(".ajitems[class='selected'] a").text());
listindex=-1;
loadSkuDetails(element);
$(ajdiv).fadeOut("slow");
}
break;
case 27:
{
listindex=-1;
$(ajdiv).fadeOut("slow");
$(actobj).focus();
}
}
}
}
else
$(ajdiv).fadeOut("slow");
});
$(ajdiv).mouseover(function(){
$(this).find(".ajitems a:first-child").mouseover(function () {
$(this).addClass("selected");
});
$(this).find(".ajitems a:first-child").mouseout(function () {
$(this).removeClass("selected");
});
$(this).find(".ajitems a:first-child").click(function () {
$(actobj).val($(this).text());
loadSkuDetails(element);
$(ajdiv).fadeOut("slow");
});
});
Run Code Online (Sandbox Code Playgroud)
};
function findPos(obj){//查找父对象的REAL位置,特别适用于滚动时
var curleft = curtop = 0;
if (obj.offsetParent) {
Run Code Online (Sandbox Code Playgroud)
做{
curleft += obj.offsetLeft;
Run Code Online (Sandbox Code Playgroud)
} while(obj = obj.offsetParent);
返回[curleft];
}
}
jQuery.fn.delay = function(time,func){//运行延迟函数
return this.each(function(){
setTimeout(func,time);
});
Run Code Online (Sandbox Code Playgroud)
};
function mvIndex(step){
if(scriptgo==1){
kids=$(".resultlist").children();
$(".resultlist").children().each(function(i){
if ($(this).hasClass("selected")){
listindex = i;console.log(listindex);
}
});
if (listindex==-1 && step==-1)change=i-1;//up = last item
if (listindex==-1 && step==1)change=0;//down = first item
if (listindex > -1){
change=listindex+step; //already selected
if (change > i-1 || change < 0) change=0;
}
console.log("mv2",listindex,"step",step,"change",change);
if (change >=0)$(".resultlist").children("*").eq(change).addClass("selected");
if (listindex >=0)$(".resultlist").children("*").eq(listindex).removeClass("selected");
scriptgo=0;
slowDown();
}
}
Run Code Online (Sandbox Code Playgroud)
function slowDown(){
$(actobj).delay(1000, function(){scriptgo=1;});}
Run Code Online (Sandbox Code Playgroud)
function fader(){
$(ajdiv).delay(10000, function(){$(ajdiv).fadeOut()});
Run Code Online (Sandbox Code Playgroud)
}
我认为你试图用for循环迭代选择而不是使用jQuery Core函数$ .each()
$(".resultlist").children().each(function(i){
if ($(this).hasClass("selected")){
listindex = i;
}
});
Run Code Online (Sandbox Code Playgroud)
上面的代码应该与您的代码尝试的代码完全相同.