简体   繁体   English

将来自ajax请求的JSON结果存储到Easyautocomplete的javascript变量中

[英]Storing JSON result from ajax request to a javascript variable for Easyautocomplete

I'm trying to implement the EasyAutoComplete plugin on a field based on the value filled in another field, using ajax requests. 我正在尝试使用ajax请求基于另一个字段中填充的值在字段上实现EasyAutoComplete插件。

I have a customerid field and when it's value changes, I want the productname field to show all products related to that customerid using the EasyAutoComplete plugin. 我有一个customerid字段,当它的值发生变化时,我希望productname字段使用EasyAutoComplete插件显示与该customerid相关的所有产品。

Here is what I have so far: 这是我到目前为止:

$('#customerid').on('change',function() {
    var products2 = {
          url: function(phrase) {
            return "database/FetchCustomerProducts.php";
        },  
        ajaxSettings: {
            dataType: "json",
            method: "POST",
            data: {
                dataType: "json"
            }
        },
        preparePostData: function(data) {
            data.phrase = $("#customerid").val();
            return data;
          },
        getValue: "name",
        list: {
            onSelectItemEvent: function() {
                var value = $("#productname").getSelectedItemData().id;
                $("#productid").val(value).trigger("change");
            },
            match: {
              enabled: true
            }
        }
    };

$("#productname").easyAutocomplete(products2);
});

Contents of FetchCustomerProducts.php: FetchCustomerProducts.php的内容:

if(!empty($_POST["customerid"])){

    $products = $app['database']->Select('products', 'customerid', $_POST['customerid']);

    echo json_encode(['data' => $products]);

}

However it's not working. 然而,它不起作用。 The code is based on the 'Ajax POST' example found on this page. 该代码基于页面上的“Ajax POST”示例。

you can using element select category add is class "check_catogory" after using event click element select get value is option, continue send id to ajax and in file php, you can get $_POST['id'] or $_GET['id'], select find database,after echo json_encode 你可以使用元素选择类别添加是类“check_catogory”后使用事件点击元素选择获取值是选项,继续发送id到ajax和文件php,你可以得到$ _POST ['id']或$ _GET ['id' ],在echo json_encode之后选择find database

$("#customerid").change(function(){
    var id = $(this).val();
    if(id!=""){
        $.ajax({
            url:"database/FetchCustomerProducts.php",
            type:"POST",
            data:{id:id},
            dataType: "json",
            cache:false,
            success:function(result){
                 var options = {
                    data:result,
                    getValue: "name",
                    list: {

                        onSelectItemEvent: function() {
                            var value = $("#productname").getSelectedItemData().id;
                            $("#productid").val(value).trigger("change");
                        },

                        match: {

                          enabled: true

                        }

                    }
                };
                $("#productname").easyAutocomplete(options);

            }
        })
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM