如果从 ajax 生成,jquery 自动完成不起作用
如果使用普通的静态 html/php 页面,自动完成功能可以正常工作,正如预期的那样。
但我正在开发一个模块,该模块可以动态加载(AJAX)生成与上面相同的 HTML 到一个区域中。我无法让自动完成功能正常工作。
知道如何解决这个问题吗?我到处搜索并尝试了一切,但显然还不是正确的解决方案。
//
// this is the ajax code load dynamic contents in a #display_area
// from onclick=selected_purchase()
//
function selected_purchase() {
var fields = $(":input").serialize();
$.ajax({
url: "purchase4",
type: "POST",
dataType: "html",
data: fields ,
beforesend: function(a) {
// before send process here
showBusy();
},
success: function(html) {
// success process here ...
processForm(html);
}
});
}
//
// this is the autocomplete code
//
$( "#supplier" ).autocomplete({ //the recipient text field with id #supplier
source: function( request, response ) {
$.ajax({
url: "search_supplier",
dataType: "json",
data: request,
success: function(data){
if(data.response == 'true') {
response(data.message);
}
}
});
},
minLength: 2,
select: function( event, ui ) {
// Do something extra on select...
// add user id to hidden input
$('input[name="name"]').val(ui.item.value);
$('input[name="sl"]').val(ui.item.sl_id);
return false;
},
});
Autocomplete it's working perfectly, as expected if using a normal statics html/php page.
But I'm working on a module that loads dynamically (AJAX) generate same HTML as above into a area. I cannot get the autocomplete to work.
Any idea of how can I solve this? I've searched everywhere and tried everything, but obviously not the right solution yet.
//
// this is the ajax code load dynamic contents in a #display_area
// from onclick=selected_purchase()
//
function selected_purchase() {
var fields = $(":input").serialize();
$.ajax({
url: "purchase4",
type: "POST",
dataType: "html",
data: fields ,
beforesend: function(a) {
// before send process here
showBusy();
},
success: function(html) {
// success process here ...
processForm(html);
}
});
}
//
// this is the autocomplete code
//
$( "#supplier" ).autocomplete({ //the recipient text field with id #supplier
source: function( request, response ) {
$.ajax({
url: "search_supplier",
dataType: "json",
data: request,
success: function(data){
if(data.response == 'true') {
response(data.message);
}
}
});
},
minLength: 2,
select: function( event, ui ) {
// Do something extra on select...
// add user id to hidden input
$('input[name="name"]').val(ui.item.value);
$('input[name="sl"]').val(ui.item.sl_id);
return false;
},
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能必须在加载内容后初始化插件实例。
此代码可能会有所不同,具体取决于您用于加载内容的方法($.ajax、.load()...)。常见的事情是在异步请求完成后执行的回调中初始化您的插件,通常称为“成功”回调。
现在我们有了您的代码示例,您应该执行以下操作:
You probably have to initialize the plugin instance after the content has been loaded.
This code might differ a bit depending on the method you use for loading the content ($.ajax, .load()...). The common thing is to initialize your plugin in the callback executed after the asynchronous request has finished, usually called the "success" callback.
Now we have your code samples, here's what you should do:
你的 Php 脚本(我猜)正在发送 JSon ?这是常见的问题,您的输出应该看起来像
您可以使用
echo json_encode($myArrayWithStringValues)
获得相同的结果。Your Php script (I guess) is sending JSon ? That's the usual problem, your outstream should look like
You could use
echo json_encode($myArrayWithStringValues)
for the same result.