jQuery 自动完成:从“源”中选择 Id;事件
我试图同时将 jQuery UI 自动完成应用于三个输入框,因为请求和逻辑几乎相同,只有一个参数在它们之间发生变化。
由于我使用的是通过 ajax 检索的远程源,因此我试图知道请求是从哪个文本框发出的。
正如您在“source”事件的“switch”语句中看到的那样,我尝试了 $(this).attr("id") 但这不起作用,它返回“indefine”
我尝试了这个,因为它适用于“ select”和“focus”事件,但不在“source”上。我想我使用它的方式是指向“源”
有谁知道一种方法可以知道在这种情况下调用的事件是从哪个元素中调用的?
谢谢!!
$("#campo-categorias, #campo-tipos, #campo-colonias").autocomplete({
minLength: 1,
delay: 100,
source: function(request, response){
var solicitud = new Object;
switch ($(this).attr("id")){
case "campo-categorias":
solicitud.action = 'get_categorias';
break;
case "campo-tipos":
solicitud.action = 'get_tipos_comida';
break;
case "campo-colonias":
solicitud.action = 'get_colonias';
break;
}
solicitud.consulta = request.term;
$.ajax({
url: "wp-admin/admin-ajax.php",
dataType: "json",
data: solicitud,
type: "POST",
success: function(data){
response(data);
}
});
});
I'm trying to apply jQuery UI autocomplete to three input boxes at the same time since the request and logic is almost the same, only one parameter changes between them.
Since I'm using a remote source that is being retrieved with ajax I'm trying to be able to know from which textbox the request was made.
As you can see in the 'switch' statement at the 'source' event I tried $(this).attr("id") but this doesn't work, it returns 'indefined'
I tried this because it worked on the 'select' and 'focus' events, but not on 'source'. I guess the way I'm using it I'm pointing to the 'source'
Does anyone know a way to know from which element was the event called in this case?
Thanks!!
$("#campo-categorias, #campo-tipos, #campo-colonias").autocomplete({
minLength: 1,
delay: 100,
source: function(request, response){
var solicitud = new Object;
switch ($(this).attr("id")){
case "campo-categorias":
solicitud.action = 'get_categorias';
break;
case "campo-tipos":
solicitud.action = 'get_tipos_comida';
break;
case "campo-colonias":
solicitud.action = 'get_colonias';
break;
}
solicitud.consulta = request.term;
$.ajax({
url: "wp-admin/admin-ajax.php",
dataType: "json",
data: solicitud,
type: "POST",
success: function(data){
response(data);
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在 switch 语句中寻找
this.element.attr("id")
。这应该可以找到正确的 id。另外,您的代码缺少大括号,它应该如下所示:
I think you are looking for
this.element.attr("id")
in your switch statement. That should work for finding the correct id.Also, your code was missing a curly bracket, it should look like below: