此错误:"未捕获TypeError:字符串不是函数"正在回复我试图做的Ajax电话。
代码是:
$('input.js-nome-produto-servico').live("keyup", function(ed, h){
var $campo = ed.currentTarget;
$campo.autocomplete({
source: "<%=Rails.application.routes.url_helpers.busca_todos_produtos_servicos_por_nome_comercial_produto_servico_oportunidades_path%>?nome="+$campo.value,
minLength: 2,
change: function(event, ui) {
bindLoadingAnimation();
},
select: function( event, ui ) {
preencheCamposCliente(campo);
}
});
});
$ campo.autocomplete上的错误正在上升。
我知道我应该打电话:
$("#field-id").autocomplete({ .........
但我有几个领域必须回应相同的&#34; keyup&#34;事件以同样的方式使用同一个类(js-nome-produto-servico)。
所以我试图从keyup函数(ed)获取DOM对象(输入)。当我调用调试器时,变量$ campo具有正确的对象,但自动完成功能不起作用。
我想我在这里缺少一些JS / JQuery概念。任何人都可以给我正确的方法吗?
非常感谢!
答案 0 :(得分:0)
您是否尝试过“搜索”活动?像:
$('.your_class').autocomplete({
source: "<%=Rails.application.routes.url_helpers.busca_todos_produtos_servicos_por_nome_comercial_produto_servico_oportunidades_path%>?nome="+ $(this).val(),
//below I replace the Minlength
search: function(){
if ( $(this).val().length < 2 ) {
return false;
}
}
change: function(event, ui) {
bindLoadingAnimation();
},
select: function( event, ui ) {
preencheCamposCliente(campo);
}
});
看看它是否有效?
答案 1 :(得分:0)
如果有人遇到同样的问题,解决方法是使用$(this)标签。所以代码是:
$('input.js-nome-produto-servico').live("keyup", function(ed, h){
$(this).autocomplete({
source: "<%=Rails.application.routes.url_helpers.busca_todos_produtos_servicos_por_nome_comercial_produto_servico_oportunidades_path%>?nome="+$(this).val(),
minLength: 2,
change: function(event, ui) {
bindLoadingAnimation();
},
select: function( event, ui ) {
preencheCamposCliente($(this));
}
});});
$(this)是受'keyup'事件影响的对象。