如果从 ajax 生成,jquery 自动完成不起作用

发布于 2025-01-06 05:28:10 字数 1559 浏览 0 评论 0原文

如果使用普通的静态 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终止放荡 2025-01-13 05:28:10

您可能必须在加载内容后初始化插件实例。

$("#container").load('some url', function() {
    $(this).find('#autocomplete').autocomplete();
});

此代码可能会有所不同,具体取决于您用于加载内容的方法($.ajax、.load()...)。常见的事情是在异步请求完成后执行的回调中初始化您的插件,通常称为“成功”回调。


现在我们有了您的代码示例,您应该执行以下操作:

function selected_purchase() {
    var fields = $(":input").serialize();
    $.ajax({
        ...
        success: function(html) {
            // success process here ...
            processForm(html);

            // assuming "processForm" append the "html" to the DOM
            // you can now call autocomplete
            $( "#supplier" ).autocomplete({
                ...
            });
        }
    });
}​

You probably have to initialize the plugin instance after the content has been loaded.

$("#container").load('some url', function() {
    $(this).find('#autocomplete').autocomplete();
});

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:

function selected_purchase() {
    var fields = $(":input").serialize();
    $.ajax({
        ...
        success: function(html) {
            // success process here ...
            processForm(html);

            // assuming "processForm" append the "html" to the DOM
            // you can now call autocomplete
            $( "#supplier" ).autocomplete({
                ...
            });
        }
    });
}​
触ぅ动初心 2025-01-13 05:28:10

你的 Php 脚本(我猜)正在发送 JSon ?这是常见的问题,您的输出应该看起来像

['toto','titi','tata']

您可以使用 echo json_encode($myArrayWithStringValues) 获得相同的结果。

Your Php script (I guess) is sending JSon ? That's the usual problem, your outstream should look like

['toto','titi','tata']

You could use echo json_encode($myArrayWithStringValues) for the same result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文